Users Pricing

blog

home / developersection / blogs / copy content of one file to another file using java

Copy content of one file to another file using java

Vijay Shukla 4070 04 Oct 2013 Updated 18 Sep 2014

In this blog I am providing you the code for Copying content of one file to another file using java.

Below line of code is help to copy the content of source file to destination file.

import java.io.*;
public class CopyClass {
   public static void main(String[] args)
{
   try
     {
      InputStream in = new FileInputStream
      (new File("Source File"));
      OutputStream out = new FileOutputStream
      (new File("Destination File"));
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
         out.write(buf, 0, len);
                System.out.println("File copied successfully!");
      }
      in.close();
      out.close();
      BufferedReader in1 = new BufferedReader
      (new FileReader("destnfile"));
      String str;
      while ((str = in1.readLine()) != null) {
         System.out.println(str);
      }
      in.close();
    }
    catch(Exception ex){}
}
}


Note: - Before run above code you must be sure your source and destination file is available or not if this file is not available then you must create above file.


Vijay Shukla

Other