Class OutputHelper

java.lang.Object
edu.gvsu.kurmasz.warszawa.io.OutputHelper

public class OutputHelper
extends java.lang.Object
Shortcuts for opening PrintWriters. In particular, this class contains shortcuts for
  • Opening a PrintWriter with autoflush either on or off
  • Opening a PrintWriter with a specified character set
  • Opening a PrintWriter to the standard output or standard error using common names like "-" and "stderr".
  • Attempting to open a PrintWriter and quitting on failure
Author:
Zachary Kurmas
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.util.Map<java.lang.String,​java.io.OutputStream> DEFAULT_OUTPUT_STREAM_MAP
    An immutable copy of the map returned by makeDefaultOutputStreamMap()
  • Constructor Summary

    Constructors 
    Constructor Description
    OutputHelper()  
  • Method Summary

    Modifier and Type Method Description
    static java.io.OutputStream getOutputStream​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map)
    Returns the OutputStream contained in the map, if present, or creates a new OutputStream attached to the specified file.
    static java.io.OutputStream getOutputStreamOrQuit​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map)
    Calls getOutputStream(String, java.util.Map) and quits if the method cannot open the requested file.
    static java.io.OutputStream getOutputStreamOrQuit​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.io.PrintStream error, int exitValue)
    Calls getOutputStream(String, java.util.Map) and quits if the method cannot open the requested file.
    static java.util.Map<java.lang.String,​java.io.OutputStream> makeDefaultOutputStreamMap()
    Generates a map of common names for the standard output and standard error to System.out and System.err respectively.
    static java.io.PrintWriter openMappedWriter​(java.lang.String filename, java.lang.String charset, boolean autoflush)
    static java.io.PrintWriter openMappedWriter​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)
    static java.io.PrintWriter openMappedWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush)
    Calls openMappedWriter(String, String, boolean), or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openMappedWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush, java.io.PrintStream error, int exitValue)
    Calls openMappedWriter(String, String, boolean), or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriter​(java.io.File file, boolean autoflush)
    Returns a PrintWriter attached to the file with the the default character set specified autoflush.
    static java.io.PrintWriter openWriter​(java.io.File file, java.lang.String charset)
    Returns a PrintWriter attached to the file with the the specified character set and without automatic line flushing.
    static java.io.PrintWriter openWriter​(java.io.File file, java.lang.String charset, boolean autoflush)
    Returns a PrintWriter attached to the file with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to the file with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.io.OutputStream stream, boolean autoflush)
    Returns a PrintWriter attached to the stream with the the default character set and autoflush.
    static java.io.PrintWriter openWriter​(java.io.OutputStream stream, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to the stream with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.lang.String filename, boolean autoflush)
    Returns a PrintWriter attached to the file with the the default character set and specified autoflush.
    static java.io.PrintWriter openWriter​(java.lang.String filename, java.lang.String charset)
    Returns a PrintWriter attached to the file with the the specified character set and no automatic line flushing.
    static java.io.PrintWriter openWriter​(java.lang.String filename, java.lang.String charset, boolean autoflush)
    Returns a PrintWriter attached to the file with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to the file with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.lang.String charset, boolean autoflush)
    Returns a PrintWriter attached to either the named file, or the OutputStream specified in map with the the specified character set and autoflush.
    static java.io.PrintWriter openWriter​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to either the named file, or the OutputStream specified in map with the the specified character set and autoflush.
    static java.io.PrintWriter openWriterOrQuit​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, boolean autoflush)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, boolean autoflush, java.io.PrintStream error, int exitValue)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush, java.io.PrintStream error, int exitValue)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.
    static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)
    Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • makeDefaultOutputStreamMap

      public static java.util.Map<java.lang.String,​java.io.OutputStream> makeDefaultOutputStreamMap()
      Generates a map of common names for the standard output and standard error to System.out and System.err respectively.
      Returns:
      the map
    • getOutputStream

      public static java.io.OutputStream getOutputStream​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map) throws java.io.FileNotFoundException
      Returns the OutputStream contained in the map, if present, or creates a new OutputStream attached to the specified file. DEFAULT_OUTPUT_STREAM_MAP maps common names for the standard output and standard error to System.out and System.err respectively.
      Parameters:
      filename - the name of the file to open, or one of the keys in map.
      map - a map of names to existing OutputStreams
      Returns:
      either the OutputStream in the map, or a new OutputStream.
      Throws:
      java.io.FileNotFoundException - if the requested file does not exist.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.OutputStream stream, java.nio.charset.Charset charset, boolean autoflush)
      Returns a PrintWriter attached to the stream with the the specified character set and autoflush.
      Parameters:
      stream - the OutputStream to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.OutputStream stream, boolean autoflush)
      Returns a PrintWriter attached to the stream with the the default character set and autoflush.
      Parameters:
      stream - the OutputStream to which to write
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and autoflush.
      Parameters:
      file - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.File file, java.lang.String charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and autoflush.
      Parameters:
      file - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.File file, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the default character set specified autoflush.
      Parameters:
      file - the File to which to write
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.io.File file, java.lang.String charset) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and without automatic line flushing.
      Parameters:
      file - the File to which to write
      charset - the desired character set
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and autoflush.
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, java.lang.String charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and autoflush.
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the default character set and specified autoflush.
      Parameters:
      filename - the File to which to write
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, java.lang.String charset) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to the file with the the specified character set and no automatic line flushing.
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.nio.charset.Charset charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to either the named file, or the OutputStream specified in map with the the specified character set and autoflush.
      Parameters:
      filename - the File to which to write
      map - a map of filenames to exisiting OutputStreams. One use of this feature is to map filenames like "-" and "stderr" onto the standard output.
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openWriter

      public static java.io.PrintWriter openWriter​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.lang.String charset, boolean autoflush) throws java.io.FileNotFoundException
      Returns a PrintWriter attached to either the named file, or the OutputStream specified in map with the the specified character set and autoflush.
      Parameters:
      filename - the File to which to write
      map - a map of filenames to exisiting OutputStreams. One use of this feature is to map filenames like "-" and "stderr" onto the standard output.
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openMappedWriter

      public static java.io.PrintWriter openMappedWriter​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush) throws java.io.FileNotFoundException
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • openMappedWriter

      public static java.io.PrintWriter openMappedWriter​(java.lang.String filename, java.lang.String charset, boolean autoflush) throws java.io.FileNotFoundException
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
      Throws:
      java.io.FileNotFoundException - if file cannot be opened for writing.
    • getOutputStreamOrQuit

      public static java.io.OutputStream getOutputStreamOrQuit​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map, java.io.PrintStream error, int exitValue)
      Calls getOutputStream(String, java.util.Map) and quits if the method cannot open the requested file.
      Parameters:
      filename - the name of the file to open, or one of the keys in map.
      map - a map of names to existing OutputStreams
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      either the OutputStream in the map, or a new OutputStream.
    • getOutputStreamOrQuit

      public static java.io.OutputStream getOutputStreamOrQuit​(java.lang.String filename, java.util.Map<java.lang.String,​java.io.OutputStream> map)
      Calls getOutputStream(String, java.util.Map) and quits if the method cannot open the requested file. Any error messages are written to System.err and the process exists with value 1.
      Parameters:
      filename - the name of the file to open, or one of the keys in map.
      map - a map of names to existing OutputStreams
      Returns:
      either the OutputStream in the map, or a new OutputStream.
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      file - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      file - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush, java.io.PrintStream error, int exitValue)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openMappedWriterOrQuit

      public static java.io.PrintWriter openMappedWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush, java.io.PrintStream error, int exitValue)
      Calls openMappedWriter(String, String, boolean), or quits if the specified file cannot be opened for writing.
      Parameters:
      filename - the file to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      the new PrintWriter
    • openMappedWriterOrQuit

      public static java.io.PrintWriter openMappedWriterOrQuit​(java.lang.String filename, java.lang.String charset, boolean autoflush)
      Calls openMappedWriter(String, String, boolean), or quits if the specified file cannot be opened for writing.
      Parameters:
      filename - the file to which to write
      charset - the desired character set
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, boolean autoflush, java.io.PrintStream error, int exitValue)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      error - the stream to which to write any error messages before quitting.
      exitValue - the value passed to System.exit
      Returns:
      the new PrintWriter
    • openWriterOrQuit

      public static java.io.PrintWriter openWriterOrQuit​(java.lang.String filename, boolean autoflush)
      Returns a PrintWriter attached to the file, or quits if the specified file cannot be opened for writing. (See openWriter(java.io.File, String, boolean).
      Parameters:
      filename - the File to which to write
      autoflush - if true, the println, printf, or format methods will flush the output buffer.
      Returns:
      the new PrintWriter