New to Java Help with 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:
Code java:
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) {}
}
}
Re: New to Java Help with Clipboard
For future reference, please use the code tags.
Break the problem down into what you need...1) read the file into memory (see The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings) ). 2) pass the file contents as a String to the Clipbard as a StringSelection object (use value from 1) to do so).
Re: New to Java Help with Clipboard
Nice to know I spent my time trying to help only to have it wasted...at least have the courtesy to mark your thread as solved.
This thread has been cross posted here:
http://www.java-forums.org/new-java/43857-text-file-clipboard.html
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting