Results 1 to 4 of 4

Thread: How to copy a file in JAVA?

  1. #1
    Join Date
    Feb 2009
    Posts
    8

    How to copy a file in JAVA?

    Hi,

    I am trying to copy a file in java but not understanding what i am missing?
    Can anyone help me with this?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How to copy a file in JAVA?

    Try this code!

    Code:
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Copy {
      public static void main(String[] args) throws IOException {
        File inputFile = new File("farrago.txt");
        File outputFile = new File("outagain.txt");
    
        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        int c;
    
        while ((c = in.read()) != -1)
          out.write(c);
    
        in.close();
        out.close();
      }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to copy a file in JAVA?

    Copy a File

    Code:
    import java.io.*;
    
         public class jCOPY {
             public static void main(String args[]){
                 try {
                 jCOPY j = new jCOPY();
                 j.CopyFile(new File(args[0]),new File(args[1]));
             }
                 catch (Exception e) {
                 e.printStackTrace();
             }
         }
        
             public void CopyFile(File in, File out) throws Exception {
             FileInputStream fis = new FileInputStream(in);
             FileOutputStream fos = new FileOutputStream(out);
             byte[] buf = new byte[1024];
             int i = 0;
                 while((i=fis.read(buf))!=-1) {
                 fos.write(buf, 0, i);
             }
             fis.close();
             fos.close();
         }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to copy a file in JAVA?

    Java does not provide a standard method to copy a file. To implement copying you need to read all bytes from source file and write to destination. The read() method will return -1 when of is reached, otherwise it returns the number of bytes read.

    Code:
       1.      public static void copy(File source, File destination) 
       2.         throws IOException 
       3.      { 
       4.         // Open file to be copied 
       5.         InputStream in = new FileInputStream(source); 
       6.       
       7.         // And where to copy it to 
       8.         OutputStream out = new FileOutputStream(destination); 
       9.       
      10.         // Read bytes and write to destination until eof 
      11.       
      12.         byte[] buf = new byte[1024]; 
      13.         int len = 0; 
      14.         while ((len = in.read(buf)) > 0) 
      15.         { 
      16.            out.write(buf, 0, len); 
      17.         } 
      18.       
      19.         // close both streams 
      20.       
      21.
             in.close(); 
      22.
             out.close(); 
      23.
          }

Similar Threads

  1. Replies: 5
    Last Post: 13-06-2012, 02:04 PM
  2. batch file to copy file to another place
    By bomblast in forum Windows Software
    Replies: 1
    Last Post: 03-11-2011, 12:53 AM
  3. Replies: 4
    Last Post: 25-10-2010, 11:59 AM
  4. Replies: 3
    Last Post: 12-03-2009, 12:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,391,124.27541 seconds with 16 queries