Results 1 to 3 of 3
  1. #1
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Question 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.

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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  3. #3
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 03-25-2011, 03:36 PM
  2. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  3. Replies: 5
    Last Post: 12-23-2009, 12:36 PM
  4. Copy a file to a folder.
    By leric in forum New To Java
    Replies: 7
    Last Post: 07-29-2009, 06:11 AM
  5. java file copy
    By hknyo in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 05:42 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
  •