Home > DeveloperSection > Blogs > Copy content of one file to another file using java

Copy content of one file to another file using java


Java Java 
Ratings:
0 Comment(s)
 2046  View(s)
Rate this:

Copy content of one file to another file using java

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.


Don't want to miss updates? Please click the below button!

Follow MindStick