Web | This Site

Java Basics Examples
Java Tutorial
Java Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Java Examples - Copy a file

## Problem Description:

How to copy one file into another file?

## Solution:

This example shows how to copy contents of one file into another file using read & write methods of BufferedWriter class.

    import java.io.*;

    public class Main {
       public static void main(String[] args)
       throws Exception {
          BufferedWriter out1 = new BufferedWriter
          (new FileWriter("srcfile"));
          out1.write("string to be copied\n");
          out1.close();
          InputStream in = new FileInputStream
          (new File("srcfile"));
          OutputStream out = new FileOutputStream
          (new File("destnfile"));
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
             out.write(buf, 0, len);
          }
          in.close();
          out.close();
          BufferedReader in1 = new BufferedReader
          (new FileReader("destnfile"));
          String str;
          while ((str = in1.readLine()) != null) {
             System.out.println(str);
          }
          in.close();
       }
    }

## Result:

The above code sample will produce the following result.

    string to be copied

Advertisement

Online Image Processing

Indian Baby Names