/** * After this method is called, output will be sent to outputStream, provided it is non-null. If * outputStream is null, then this method has the same effect as calling writeStandardOutput(); * that is, future output will be sent to the standard output stream. */ public static void writeStream(PrintWriter outputStream) { if (outputStream == null) writeStandardOutput(); else { out = outputStream; outputFileName = null; outputErrorCount = 0; writingStandardOutput = false; } }
/** * Opens a file with a specified name for output. If the file name is null, this has the same * effect as calling writeStandardOutput(); that is, output will be sent to standard output. If an * error occurs while trying to open the file, an exception of type IllegalArgumentException is * thrown. If the file is opened successfully, then after this method is called, all of the output * routines will write to the file, instead of to standard output. If an error occurs, the output * destination is not changed. * * <p>NOTE: Calling this method starts a GUI user interface thread, which can continue to run even * if the thread that runs the main program ends. If you use this method in a non-GUI program, it * might be necessary to call System.exit(0) at the end of the main() routine to shut down the * Java virtual machine completely. */ public static void writeFile(String fileName) { if (fileName == null) // Go back to reading standard output writeStandardOutput(); else { PrintWriter newout; try { newout = new PrintWriter(new FileWriter(fileName)); } catch (Exception e) { throw new IllegalArgumentException( "Can't open file \"" + fileName + "\" for output.\n" + "(Error :" + e + ")"); } if (!writingStandardOutput) { try { out.close(); } catch (Exception e) { } } out = newout; writingStandardOutput = false; outputFileName = fileName; outputErrorCount = 0; } }
/** * After this method is called, output will be sent to outputStream, provided it is non-null. If * outputStream is null, then this method has the same effect as calling writeStandardOutput(); * that is, future output will be sent to the standard output stream. */ public static void writeStream(OutputStream outputStream) { if (outputStream == null) writeStandardOutput(); else writeStream(new PrintWriter(outputStream)); }