// load Java's I/O facilities: import java.io.*; public class FileIO { // $main$ must "throw" an exception if input is illegal: public static void main(String[] args) throws IOException { // create object $in$ that allows you to prompt user for input: BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); // read strings: System.out.print("Enter file name to read: "); String inFileName = in.readLine(); System.out.print("Enter file name for output: "); String outFileName = in.readLine(); // ensure that the input file exists: while (!new File(inFileName).exists()) { System.out.println("No such file !"); System.out.print("Enter file name to read: "); inFileName = in.readLine(); } // generate an input stream to read from the file: BufferedReader inFile = new BufferedReader (new FileReader(inFileName)); // generate: an output stream to write to the output file: PrintWriter outFile=new PrintWriter (new FileWriter(new File(outFileName))); // read strings from input file and write to output file: String s = inFile.readLine(); while (s != null) { System.out.println("Here is the string I read: "+s); outFile.println("Recording the string I read: "+s); s = inFile.readLine(); // get next string } // close files when finished: inFile.close(); outFile.close(); } }