java - 从剪贴板到文件

标签 java clipboard text-files

我想从剪贴板获取数据并将其存储在 .txt 文件中。

如何创建 .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;

}
 }

最佳答案

我已经用当前代码解决了这个问题,感谢提示

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();
        }
      }

关于java - 从剪贴板到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979323/

相关文章:

java - 从通用抽象类java返回子类

seo - 如何创建 google xml 站点地图和 robots.txt 文件?

java - 将 javafx 文本区域写入带有换行符的文本文件

JavaFX 操作两个 FXML 中的标签

r - 将文本转换为表格,分隔符为#

java - 玩 war 命令不起作用

java - 确保任务是可中断的

java - 在 Java 中将列表转换为具有正确扩展名的 JSON

java - JDK11 的 checkSystemClipboardAccess 替代方案

Java clipboardOwner 目的?