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
PrintWriters. In particular, this class contains shortcuts for
- Opening a
PrintWriterwith autoflush either on or off - Opening a
PrintWriterwith a specified character set - Opening a
PrintWriterto the standard output or standard error using common names like "-" and "stderr". - Attempting to open a
PrintWriterand 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_MAPAn immutable copy of the map returned bymakeDefaultOutputStreamMap() -
Constructor Summary
Constructors Constructor Description OutputHelper() -
Method Summary
Modifier and Type Method Description static java.io.OutputStreamgetOutputStream(java.lang.String filename, java.util.Map<java.lang.String,java.io.OutputStream> map)Returns theOutputStreamcontained in themap, if present, or creates a newOutputStreamattached to the specified file.static java.io.OutputStreamgetOutputStreamOrQuit(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.OutputStreamgetOutputStreamOrQuit(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.outandSystem.errrespectively.static java.io.PrintWriteropenMappedWriter(java.lang.String filename, java.lang.String charset, boolean autoflush)static java.io.PrintWriteropenMappedWriter(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)static java.io.PrintWriteropenMappedWriterOrQuit(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.PrintWriteropenMappedWriterOrQuit(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.PrintWriteropenWriter(java.io.File file, boolean autoflush)Returns aPrintWriterattached to thefilewith the the default character set specified autoflush.static java.io.PrintWriteropenWriter(java.io.File file, java.lang.String charset)Returns aPrintWriterattached to thefilewith the the specified character set and without automatic line flushing.static java.io.PrintWriteropenWriter(java.io.File file, java.lang.String charset, boolean autoflush)Returns aPrintWriterattached to thefilewith the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to thefilewith the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.io.OutputStream stream, boolean autoflush)Returns aPrintWriterattached to thestreamwith the the default character set and autoflush.static java.io.PrintWriteropenWriter(java.io.OutputStream stream, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to thestreamwith the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.lang.String filename, boolean autoflush)Returns aPrintWriterattached to the file with the the default character set and specified autoflush.static java.io.PrintWriteropenWriter(java.lang.String filename, java.lang.String charset)Returns aPrintWriterattached to the file with the the specified character set and no automatic line flushing.static java.io.PrintWriteropenWriter(java.lang.String filename, java.lang.String charset, boolean autoflush)Returns aPrintWriterattached to the file with the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to the file with the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.lang.String filename, java.util.Map<java.lang.String,java.io.OutputStream> map, java.lang.String charset, boolean autoflush)Returns aPrintWriterattached to either the named file, or theOutputStreamspecified inmapwith the the specified character set and autoflush.static java.io.PrintWriteropenWriter(java.lang.String filename, java.util.Map<java.lang.String,java.io.OutputStream> map, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to either the named file, or theOutputStreamspecified inmapwith the the specified character set and autoflush.static java.io.PrintWriteropenWriterOrQuit(java.io.File file, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.io.File file, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, boolean autoflush)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, boolean autoflush, java.io.PrintStream error, int exitValue)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, java.lang.String charset, boolean autoflush)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, java.lang.String charset, boolean autoflush, java.io.PrintStream error, int exitValue)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush)Returns aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing.static java.io.PrintWriteropenWriterOrQuit(java.lang.String filename, java.nio.charset.Charset charset, boolean autoflush, java.io.PrintStream error, int exitValue)Returns aPrintWriterattached 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.outandSystem.errrespectively.- 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 theOutputStreamcontained in themap, if present, or creates a newOutputStreamattached to the specified file.DEFAULT_OUTPUT_STREAM_MAPmaps common names for the standard output and standard error toSystem.outandSystem.errrespectively.- 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
OutputStreamin 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 aPrintWriterattached to thestreamwith the the specified character set and autoflush.- Parameters:
stream- theOutputStreamto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter
-
openWriter
public static java.io.PrintWriter openWriter(java.io.OutputStream stream, boolean autoflush)Returns aPrintWriterattached to thestreamwith the the default character set and autoflush.- Parameters:
stream- theOutputStreamto which to writeautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefilewith the the specified character set and autoflush.- Parameters:
file- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 aPrintWriterattached to thefilewith the the specified character set and autoflush.- Parameters:
file- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.io.File file, boolean autoflush) throws java.io.FileNotFoundExceptionReturns aPrintWriterattached to thefilewith the the default character set specified autoflush.- Parameters:
file- theFileto which to writeautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.io.File file, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns aPrintWriterattached to thefilewith the the specified character set and without automatic line flushing.- Parameters:
file- theFileto which to writecharset- the desired character set- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 aPrintWriterattached to the file with the the specified character set and autoflush.- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 aPrintWriterattached to the file with the the specified character set and autoflush.- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.lang.String filename, boolean autoflush) throws java.io.FileNotFoundExceptionReturns aPrintWriterattached to the file with the the default character set and specified autoflush.- Parameters:
filename- theFileto which to writeautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot be opened for writing.
-
openWriter
public static java.io.PrintWriter openWriter(java.lang.String filename, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns aPrintWriterattached to the file with the the specified character set and no automatic line flushing.- Parameters:
filename- theFileto which to writecharset- the desired character set- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 aPrintWriterattached to either the named file, or theOutputStreamspecified inmapwith the the specified character set and autoflush.- Parameters:
filename- theFileto 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, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 aPrintWriterattached to either the named file, or theOutputStreamspecified inmapwith the the specified character set and autoflush.- Parameters:
filename- theFileto 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, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter - Throws:
java.io.FileNotFoundException- iffilecannot 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 existingOutputStreamserror- the stream to which to write any error messages before quitting.exitValue- the value passed toSystem.exit- Returns:
- either the
OutputStreamin 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.errand 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
OutputStreamin 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
file- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
file- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writecharset- the desired character setautoflush- iftrue, theprintln,printf, orformatmethods 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, orformatmethods 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, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writeautoflush- iftrue, theprintln,printf, orformatmethods 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 aPrintWriterattached to thefile, or quits if the specified file cannot be opened for writing. (SeeopenWriter(java.io.File, String, boolean).- Parameters:
filename- theFileto which to writeautoflush- iftrue, theprintln,printf, orformatmethods will flush the output buffer.- Returns:
- the new
PrintWriter
-