// This class encapsulates the details of text file IO in Java // // It has two methods: // openRead --- takes a filename (String) as a parameter and // returns a Scanner for reading from that file // openWrite -- takes a filename (String) as a parameter and // returns a PrintWriter for writing to that file // // M. Branicky, 10/09/06 import java.io.File; // needed for opening/accessing/closing files import java.util.Scanner; // needed for input processing using Scanner import java.io.PrintWriter; // needed for output processing using PrintWriter import java.io.FileNotFoundException; // needed for exception handling public class TextFileIO { public static Scanner openRead (String filename) { Scanner input = null; File file = new File(filename); try { input = new Scanner(file); } catch (FileNotFoundException f) { System.out.println(filename + " not found."); System.exit(1); } return input; } public static PrintWriter openWrite (String filename) { PrintWriter output = null; File file = new File(filename); try { output = new PrintWriter(file); } catch (FileNotFoundException f) { System.out.println("Could not open "+filename); System.exit(1); } return output; } }