Package com.globalmentor.io
Class PrintStreamWriter
java.lang.Object
java.io.Writer
com.globalmentor.io.PrintStreamWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
Converts a
PrintStream
to a Writer
.
This class delegates to PrintStream.print(String)
for writing strings, maintaining the charset of the underlying print stream. This is especially
important to maintain the charset of System.out
and System.err
, which virtually impossible to determine especially in earlier versions of
Java. Contrast this with PrintWriter
, which does not maintain the underlying charset of the PrintStream
.
- Author:
- Garret Wilson
- API Note:
- Note that if
System.out
orSystem.err
is being redirected, the charset may continue to be set to the console charset, which may not necessarily be the system default charset and may not be appropriate for saving to a file. This is a Java limitation and is for the most part unavoidable. It may thus be better to avoid redirecting output and instead provide options for explicitly saving to an external file with some specified or default charset. See How to find charset of System.err if stdout is redirected? for more discussion. - See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionPrintStreamWriter
(PrintStream printStream) Print stream constructor.PrintStreamWriter
(PrintStream printStream, boolean closeStreamOnClose) Print stream and optional closing constructor. -
Method Summary
-
Constructor Details
-
PrintStreamWriter
Print stream constructor.- API Note:
- If writing to
System.out
orSystem.err
it probably more appropriate to use thePrintStreamWriter(PrintStream, boolean)
constructor with a close argument offalse
. - Parameters:
printStream
- The print stream to convert.- See Also:
-
PrintStreamWriter
Print stream and optional closing constructor.- API Note:
- Setting the close option to
false
is useful if wrappingSystem.out
orSystem.err
, as these streams are provided by the JVM and should not be closed by the application. - Parameters:
printStream
- The print stream to convert.closeStreamOnClose
-true
if the given print stream should be closed when the writer is closed, orfalse
if the print stream should be left open.- See Also:
-
-
Method Details
-
write
- Overrides:
write
in classWriter
- Throws:
IOException
-
write
- Overrides:
write
in classWriter
- Throws:
IOException
-
write
- Specified by:
write
in classWriter
- Throws:
IOException
-
write
- Overrides:
write
in classWriter
- Throws:
IOException
-
write
- Overrides:
write
in classWriter
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classWriter
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
-