/* Punters Lounge JAVA programming course * * Lesson7c.java * * Author : Erik Datapunter * Date : 12/jun/2004 * * Reading from a file and writing to a file using buffers * */ // import the library with Input Output objects import java.io.*; public class Lesson7c { public static void main(String[ ] arguments) throws Exception { /* create a file object of the file to read * create a FileReader object * create a BufferedReader object * we can access the file with the inputfilebuffer object */ File FromFile = new File("in-file.txt"); FileReader ReadFile = new FileReader(FromFile); BufferedReader inputfilebuffer = new BufferedReader(ReadFile); /* create a file object of the file to write * create a FileWriter object * create a BufferedWriter object * we can access the file with the outputfilebuffer object */ File ToFile = new File("out-file.txt"); FileWriter WriteFile = new FileWriter(ToFile); BufferedWriter outputfilebuffer = new BufferedWriter(WriteFile); // a String to hold a single line String ReadLine = ""; // loop as long as there are lines to read from the input file while ((ReadLine = inputfilebuffer.readLine()) != null) { // write the line to the output file outputfilebuffer.write(ReadLine); outputfilebuffer.newLine(); System.out.println(ReadLine); } // when all lines have been read and written close the buffer // this will automatically flush the Writer buffer to the hard-disk and close the file inputfilebuffer.close(); outputfilebuffer.close(); } }