/******************* * CS211 Fall 2003 * * CS211Out.java * *******************/ import java.io.*; public class CS211Out implements CS211OutInterface { String FileName; // Output filename PrintWriter out; // generic output device (file and cmd-line) boolean errorp = false; // error flag (so far, no problem) boolean autoflush = true; // flag for automatically flushing output // Open file for output: public CS211Out (String FileName) { try { this.FileName = FileName; out = new PrintWriter(new FileWriter(new File(FileName)),autoflush); } catch (Exception e) { System.out.println("ERROR: Unable to open file " + FileName + " for output\n"); errorp = true; } } // Report output at the command-line: public CS211Out() { out = new PrintWriter(System.out,autoflush); } // Close file: public void close () { try { out.close(); } catch (Exception e) { if (!errorp) System.out.println("Error: Unable to close file " + FileName); errorp = true; } } // Print methods for a variety of types: public void println() { out.println(); } public void println(String s) { out.println(s); } public void println(int i) { out.println(i); } public void println(double d) { out.println(d); } public void println(float f) { out.println(f); } public void println(char c) { out.println(c); } public void println(Object o) { out.println(o); } // Why flush? The PW class flushes the buffer *after* a println has exectuted! public void print(String s) { out.print(s); out.flush(); } public void print(int i) { out.print(i); out.flush(); } public void print(double d) { out.print(d); out.flush(); } public void print(float f) { out.print(f); out.flush(); } public void print(char c) { out.print(c); out.flush(); } public void print(Object o) { out.print(o); out.flush(); } }