2011-05-12 92 views
2

我想从剪贴板获取数据并将其存储在.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; 

} 
} 
+3

它看起来像你能得到你的数据作为一个字符串,做一个快速搜索,看看如何写字符串的文件。 – user489041 2011-05-12 14:17:56

+2

你的具体问题是什么? – jzd 2011-05-12 14:19:50

+0

查看http://download.oracle.com/javase/tutorial/essential/io/ – Koekiebox 2011-05-12 14:23:36

回答

2

我已经与当前的代码感谢名单解决它的提示

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

看看FileWriterBufferedWriter的字符串写入到文件中。

+1

如果您更喜欢使用预建库,还有一个Apache Commons IO中的FileUtils.writeStringToFile(File,String)实用程序。 – 2012-03-19 23:16:02

+0

我也建议使用实用程序。个人偏好是写出.csv而不是.txt,因为你有一个文本文件和一个很容易在Excel(或类似程序)中打开的文件,请参阅[CSVwriter](http://opencsv.sourceforge.net/ ) – 2017-11-09 10:17:07