package jutil; import java.io.*; import java.util.*; /** * Utilities for console and file I/O. */ public class IOUtils { /** * Represents standard input stream; default * source is keyboard unless redirected at the * command line. */ public static final BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); /** * Represents standard output stream; default * destination is console window unless redirected * at the command line. */ public static final PrintWriter stdout = new PrintWriter( new BufferedWriter( new OutputStreamWriter(System.out)), true); /** * Represents standard error stream; default * destination is console window unless redirected * at the command line. */ public static final PrintWriter stderr = new PrintWriter( new BufferedWriter( new OutputStreamWriter(System.out)), true); /** * A factory for turning file names into character output streams. * @param fname the name of the file to be written * @return a character output stream */ public static PrintWriter makeWriter(String fname) { PrintWriter result = null; try { result = new PrintWriter( new BufferedWriter( new FileWriter(fname)), true); } catch(IOException e) { error(e.getMessage()); } return result; } /** * A factory for turning file names into character input streams. * @param fname The name of the text file to be read. * @return a character input stream */ public static BufferedReader makeReader(String fname) { BufferedReader result = null; try { result = new BufferedReader(new FileReader(fname)); } catch(IOException e){ error(e.getMessage()); } return result; } /** * Reads a line from a reader without throwing an exception. * A line is terminated by a newline char. * @param reader a character input stream * @return next line from the reader */ public static String readLine(BufferedReader reader) { String result = null; try { result = reader.readLine(); } catch(EOFException e) { } catch(IOException e) { stderr.println(e.getMessage()); } return result; } /** * Reads a line from stdin without throwing an exception. * @return next line from stdout */ public static String readLine() { return readLine(stdin); } /** * Prints a message to a character output stream without a newline. * @param writer a file output stream * @param msg the message displayed */ public static void print(PrintWriter writer,String msg) { writer.print(msg); writer.flush(); // flush any buffered chars } /** * Prints a message to a character output stream with a newline. * @param writer a file output stream * @param msg the message displayed */ public static void println(PrintWriter writer, String msg) { writer.println(msg); } /** * Prints a message to stdout without a newline. * @param msg the message displayed */ public static void print(String msg) { print(stdout, msg); } /** * Prints a message to stdout with a newline. * @param msg the message displayed */ public static void println(String msg) { println(stdout, msg); } /** * Parses a string into a sequence of tokens. * A token is a string containing no white space. * @param tokens the string to be parsed * @return a sequence of tokens. */ public static StringTokenizer tokenize(String tokens) { return new StringTokenizer(tokens); } /** * Prints an error message to stderr. * @param gripe the error message */ public static void error(String gripe) { println(stderr, "Error: " + gripe); } /** * Prints an error message to stdout then terminates the application. * @param gripe the error message */ public static void seriousError(String gripe) { error(gripe); System.exit(1); } }