Java: Reading and Writing to a File

, , …,

The following program is a example that makes a copy of file, by reading the file content char by char, then writing it out.

import java.io.*;

public class rFile {
     public static void main(String[] args) throws IOException {
         File inputFile = new File("/Users/t/t.txt");
         File outputFile = new File("/Users/t/t2.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();
     }
}
blog comments powered by Disqus