Results 1 to 3 of 3
Thread: Copy Text File to Another File
- 04-04-2011, 04:48 PM #1
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
Copy Text File to Another File
Hi, I was wondering if someone can advise why my file copy is not working, I am sure it is somethng very symple but I just can't figure it out.
Many ThanksJava Code:public class CopyLog { File f1 = new File("C:\\Temp\\original_file.txt"); File f2 = new File("C:\\Temp\\copied_file.txt"); void copy(File f1, File f2) throws IOException { InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } }
- 04-04-2011, 06:03 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
might be easier to wrap the data streams in a printwriter and a buffered reader. Then you can just read line with the buffered reader, store it in a string, and then use the printwriter to write to the new file.
What happens when you run this? Errors? nothing?
- 04-05-2011, 08:14 AM #3
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
Hi, with a couple of bit of modifications I managed to sort it out, still using my method, but thank you for the suggestion.
Java Code:public class CopyLog { public static void main(String[] args) throws IOException { File f1 = new File("C:\\Temp\\output_file.txt"); File f2 = new File("C:\\Temp\\copied_file.txt"); InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); String text = "Copied output_file.txt"; System.out.println(text); } }
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 -
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
Copy file from 1 dir to another dir of the same system
By newjava in forum New To JavaReplies: 5Last Post: 12-23-2009, 11:36 AM -
Copy a file to a folder.
By leric in forum New To JavaReplies: 7Last Post: 07-29-2009, 05:11 AM -
java file copy
By hknyo in forum New To JavaReplies: 1Last Post: 06-12-2008, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks