Results 1 to 4 of 4
Thread: Text File to Clipboard
- 05-11-2011, 01:13 AM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
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) {}
}
}
- 05-11-2011, 01:32 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Read the file into a StringBuilder. Then you create the StringSelection(...) object using the toString() method of the StringBuilder.
- 05-11-2011, 02:11 AM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Thanks for the help.... worked perfectly
- 05-11-2011, 02:42 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Similar Threads
-
Read a data from a text file and create an object from these data in this text file
By jjavaa in forum Advanced JavaReplies: 2Last Post: 03-25-2011, 02:36 PM -
Transforming xml to text keeps placing blank line at beginning of text file
By DerekRaimann in forum Advanced JavaReplies: 7Last Post: 03-05-2011, 09:25 AM -
Taking a line from a text file if it contains the specified text
By daoping in forum New To JavaReplies: 1Last Post: 02-28-2011, 05:30 PM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
Insert text records from text file into a DB
By nicedad in forum JDBCReplies: 8Last Post: 11-06-2009, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks