Friday, December 23, 2011

How to Copy a File to Another in Java?


// Plain and Simple Method

import java.io.*;

  public class FileCopy {
      public static void main(String[] args) throws IOException {
          // create two file references
          File inputFile = new File("1.txt");
          File outputFile = new File("2.txt");

          // create two File Streams
          FileReader in = new FileReader(inputFile);
          FileWriter out = new FileWriter(outputFile);

          // read one file and write it out to another
          int c;

          while ((c = in.read()) != -1)
             out.write(c);

          // close both files
          in.close();
          out.close();
      }
  }

No comments:

Post a Comment