Package edu.gvsu.kurmasz.warszawa.io
Class OutputHelper
java.lang.Object
edu.gvsu.kurmasz.warszawa.io.OutputHelper
public class OutputHelper
extends java.lang.Object
Shortcuts for opening
PrintWriter
s. 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 bymakeDefaultOutputStreamMap()
-
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 theOutputStream
contained in themap
, if present, or creates a newOutputStream
attached to the specified file.static java.io.OutputStream
getOutputStreamOrQuit(java.lang.String filename, java.util.Map<java.lang.String,java.io.OutputStream> map)
CallsgetOutputStream(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)
CallsgetOutputStream(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 toSystem.out
andSystem.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)
CallsopenMappedWriter(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)
CallsopenMappedWriter(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 aPrintWriter
attached to thefile
with the the default character set specified autoflush.static java.io.PrintWriter
openWriter(java.io.File file, java.lang.String charset)
Returns aPrintWriter
attached to thefile
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 aPrintWriter
attached to thefile
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 aPrintWriter
attached to thefile
with the the specified character set and autoflush.static java.io.PrintWriter
openWriter(java.io.OutputStream stream, boolean autoflush)
Returns aPrintWriter
attached to thestream
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 aPrintWriter
attached to thestream
with the the specified character set and autoflush.static java.io.PrintWriter
openWriter(java.lang.String filename, boolean autoflush)
Returns aPrintWriter
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 aPrintWriter
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 aPrintWriter
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 aPrintWriter
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 aPrintWriter
attached to either the named file, or theOutputStream
specified inmap
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 aPrintWriter
attached to either the named file, or theOutputStream
specified inmap
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 aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing.static java.io.PrintWriter
openWriterOrQuit(java.lang.String filename, boolean autoflush)
Returns aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, 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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing.
-
Field Details
-
DEFAULT_OUTPUT_STREAM_MAP
public static final java.util.Map<java.lang.String,java.io.OutputStream> DEFAULT_OUTPUT_STREAM_MAPAn immutable copy of the map returned bymakeDefaultOutputStreamMap()
-
-
Constructor Details
-
OutputHelper
public OutputHelper()
-
-
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 toSystem.out
andSystem.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.FileNotFoundExceptionReturns theOutputStream
contained in themap
, if present, or creates a newOutputStream
attached to the specified file.DEFAULT_OUTPUT_STREAM_MAP
maps common names for the standard output and standard error toSystem.out
andSystem.err
respectively.- Parameters:
filename
- the name of the file to open, or one of the keys inmap
.map
- a map of names to existingOutputStreams
- Returns:
- either the
OutputStream
in the map, or a newOutputStream
. - 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 aPrintWriter
attached to thestream
with the the specified character set and autoflush.- Parameters:
stream
- theOutputStream
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
-
openWriter
public static java.io.PrintWriter openWriter(java.io.OutputStream stream, boolean autoflush)Returns aPrintWriter
attached to thestream
with the the default character set and autoflush.- Parameters:
stream
- theOutputStream
to which to writeautoflush
- iftrue
, theprintln
,printf
, orformat
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.FileNotFoundExceptionReturns aPrintWriter
attached to thefile
with the the specified character set and autoflush.- Parameters:
file
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptionReturns aPrintWriter
attached to thefile
with the the specified character set and autoflush.- Parameters:
file
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
cannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.io.File file, boolean autoflush) throws java.io.FileNotFoundExceptionReturns aPrintWriter
attached to thefile
with the the default character set specified autoflush.- Parameters:
file
- theFile
to which to writeautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
cannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.io.File file, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns aPrintWriter
attached to thefile
with the the specified character set and without automatic line flushing.- Parameters:
file
- theFile
to which to writecharset
- the desired character set- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptionReturns aPrintWriter
attached to the file with the the specified character set and autoflush.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptionReturns aPrintWriter
attached to the file with the the specified character set and autoflush.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
cannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.lang.String filename, boolean autoflush) throws java.io.FileNotFoundExceptionReturns aPrintWriter
attached to the file with the the default character set and specified autoflush.- Parameters:
filename
- theFile
to which to writeautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
cannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.lang.String filename, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns aPrintWriter
attached to the file with the the specified character set and no automatic line flushing.- Parameters:
filename
- theFile
to which to writecharset
- the desired character set- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptionReturns aPrintWriter
attached to either the named file, or theOutputStream
specified inmap
with the the specified character set and autoflush.- Parameters:
filename
- theFile
to which to writemap
- a map of filenames to exisitingOutputStreams
. One use of this feature is to map filenames like "-" and "stderr" onto the standard output.charset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptionReturns aPrintWriter
attached to either the named file, or theOutputStream
specified inmap
with the the specified character set and autoflush.- Parameters:
filename
- theFile
to which to writemap
- a map of filenames to exisitingOutputStreams
. One use of this feature is to map filenames like "-" and "stderr" onto the standard output.charset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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.FileNotFoundExceptioncallsopenWriter(String, java.util.Map, java.nio.charset.Charset, boolean)
usingDEFAULT_OUTPUT_STREAM_MAP
- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
- Throws:
java.io.FileNotFoundException
- iffile
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)CallsgetOutputStream(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 inmap
.map
- a map of names to existingOutputStreams
error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- either the
OutputStream
in the map, or a newOutputStream
.
-
getOutputStreamOrQuit
public static java.io.OutputStream getOutputStreamOrQuit(java.lang.String filename, java.util.Map<java.lang.String,java.io.OutputStream> map)CallsgetOutputStream(String, java.util.Map)
and quits if the method cannot open the requested file. Any error messages are written toSystem.err
and the process exists with value 1.- Parameters:
filename
- the name of the file to open, or one of the keys inmap
.map
- a map of names to existingOutputStreams
- Returns:
- either the
OutputStream
in the map, or a newOutputStream
.
-
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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
file
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- the new
PrintWriter
-
openWriterOrQuit
public static java.io.PrintWriter openWriterOrQuit(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
file
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- the new
PrintWriter
-
openWriterOrQuit
public static java.io.PrintWriter openWriterOrQuit(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- the new
PrintWriter
-
openWriterOrQuit
public static java.io.PrintWriter openWriterOrQuit(java.lang.String filename, java.lang.String charset, boolean autoflush)Returns aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
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)CallsopenMappedWriter(String, String, boolean)
, or quits if the specified file cannot be opened for writing.- Parameters:
filename
- the file to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- the new
PrintWriter
-
openMappedWriterOrQuit
public static java.io.PrintWriter openMappedWriterOrQuit(java.lang.String filename, java.lang.String charset, boolean autoflush)CallsopenMappedWriter(String, String, boolean)
, or quits if the specified file cannot be opened for writing.- Parameters:
filename
- the file to which to writecharset
- the desired character setautoflush
- iftrue
, theprintln
,printf
, orformat
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 aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writeautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.error
- the stream to which to write any error messages before quitting.exitValue
- the value passed toSystem.exit
- Returns:
- the new
PrintWriter
-
openWriterOrQuit
public static java.io.PrintWriter openWriterOrQuit(java.lang.String filename, boolean autoflush)Returns aPrintWriter
attached to thefile
, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean)
.- Parameters:
filename
- theFile
to which to writeautoflush
- iftrue
, theprintln
,printf
, orformat
methods will flush the output buffer.- Returns:
- the new
PrintWriter
-