Из буфера обмена в файл - PullRequest
2 голосов
/ 12 мая 2011

Я хочу получить данные из буфера обмена и сохранить их в текстовом файле.

Как мне создать файл .txt? Я много читал о получении данных из файла, но не наоборот.

Вот мой код:

    public void CallClipboard (){
        System.out.println("Copying text from system clipboard.");
        String grabbed = ReadClipboard();
        System.out.println(grabbed);


    }
    public String ReadClipboard () {
        // get the system clipboard
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // get the contents on the clipboard in a 
        // transferable object
        Transferable clipboardContents = systemClipboard.getContents(null);
        // check if clipboard is empty
        if (clipboardContents.equals(null)) {

        return ("Clipboard is empty!!!");
        } 
        else

    try {
    // see if DataFlavor of 
    // DataFlavor.stringFlavor is supported

        if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        // return text content
        String returnText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);
        return returnText;
        }
    } 
    catch (UnsupportedFlavorException ufe) {
    ufe.printStackTrace();
    } 
    catch (IOException ioe) {
    ioe.printStackTrace();
    }
    return null;

}
 }

Ответы [ 2 ]

2 голосов
/ 13 мая 2011

Я решил это с текущим кодом спасибо за советы

public static void CallClipboard (String file){
    System.out.println("Copying text from system clipboard.");
    String grabbed = ReadClipboard(file);
    System.out.println(grabbed);

}
public static String ReadClipboard (String file) {
     File testFile = new File(file);
    // get the system clipboard
    Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    // get the contents on the clipboard in a 
    // transferable object
    Transferable clipboardContents = systemClipboard.getContents(null);
    // check if clipboard is empty
    if (clipboardContents.equals(null)) {

    return ("Clipboard is empty!!!");
    } 
    else

    try {
// see if DataFlavor of 
// DataFlavor.stringFlavor is supported

        if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        // return text content
            String returnText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);

            try {
                setContents(testFile, returnText);
              } 
              catch (FileNotFoundException e) {
                e.printStackTrace();
              } 
              catch (IOException e) {
                e.printStackTrace();
              }

            return returnText;


        }
    } 

    catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
    } 
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return null;
    }


  static public void setContents(File aFile, String aContents) throws FileNotFoundException, IOException {
      if (aFile == null) {
          throw new IllegalArgumentException("File should not be null.");
      }
      if (!aFile.exists()) {
          throw new FileNotFoundException ("File does not exist: " + aFile);
      }
      if (!aFile.isFile()) {
          throw new IllegalArgumentException("Should not be a directory: " + aFile);
      }
      if (!aFile.canWrite()) {
          throw new IllegalArgumentException("File cannot be written: " + aFile);
      }
    //use buffering
        Writer output = new BufferedWriter(new FileWriter(aFile));
        try {
        //FileWriter always assumes default encoding is OK!
            output.write( aContents );
        }

      finally {
            output.close();
        }
      }
1 голос
/ 12 мая 2011

Посмотрите FileWriter и BufferedWriter для записи строк в файлы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...