开发者

From clipboard to file

I want to get data from the clipboard and store it in a .txt file.

How do I create the .txt file? I have read a lot about getting data from a file but not the other way around.

Here is my code:

    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

       开发者_如何学Go 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;

}
 }


I have solved it with the current code thanx for the tips

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


Take a look at FileWriter and BufferedWriter for writing Strings to files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜