Results 1 to 4 of 4
  1. #1
    radgator is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Text File to Clipboard

    I am trying to copy a text file using scanner to the clipboard. Can someone tell me what my clipboard.setContents method should look like?

    This is what I have now:


    import java.awt.Toolkit;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.StringSelection;
    import java.io.File;
    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    File file = new File ("c:\\Temp\\testdata.txt");

    try{
    Scanner scanner = new Scanner(file);

    while (scanner.hasNextLine()) {

    String line = scanner.nextLine();

    System.out.println(line);

    }

    clipboard.setContents(new StringSelection("string"), null); //right now this just copies "string" to the clipboard. I need it to copy the scanned in file to the clipboard.

    } catch (Exception e) {}
    }

    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Read the file into a StringBuilder. Then you create the StringSelection(...) object using the toString() method of the StringBuilder.

  3. #3
    radgator is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the help.... worked perfectly

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  2. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  3. Replies: 1
    Last Post: 02-28-2011, 05:30 PM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 8
    Last Post: 11-06-2009, 06:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •