Java Reference
In-Depth Information
After a writer has been closed, further writes throw IOException s.
OutputStreamWriter
OutputStreamWriter is the most important concrete subclass of Writer . An Output
StreamWriter receives characters from a Java program. It converts these into bytes
according to a specified encoding and writes them onto an underlying output stream.
Its constructor specifies the output stream to write to and the encoding to use:
public OutputStreamWriter ( OutputStream out , String encoding )
throws UnsupportedEncodingException
Valid encodings are listed in the documentation for Sun's native2ascii tool included with
the JDK. If no encoding is specified, the default encoding for the platform is used. In
2013, the default encoding is UTF-8 on the Mac and more often than not on Linux.
However, on Linux it can vary if the local operating system is configured to use some
other character set by default. On Windows, it varies depending on country and con‐
figuration, but in the United States it's Windows-1252, a.k.a CP1252 more often than
not. Default character sets can cause unexpected problems at unexpected times. You're
generally almost always better off explicitly specifying the character set rather than
letting Java pick one for you. For example, this code fragment writes the first few words
of Homer's Odyssey in the CP1253 Windows Greek encoding:
OutputStreamWriter w = new OutputStreamWriter(
new FileOutputStream("OdysseyB.txt"), "Cp1253");
w.write("ἦμος δ΄ ἠριγένεια φάνη ῥοδοδάκτυλος Ἠώς");
Other than the constructors, OutputStreamWriter has only the usual Writer methods
(which are used exactly as they are for any Writer class) and one method to return the
encoding of the object:
public String getEncoding ()
Readers
The Reader class mirrors the java.io.InputStream class. It's abstract with two pro‐
tected constructors. Like InputStream and Writer , the Reader class is never used di‐
rectly, only through one of its subclasses. It has three read() methods, as well as skip() ,
close() , ready() , mark() , reset() , and markSupported() methods:
protected Reader ()
protected Reader ( Object lock )
public abstract int read ( char [] text , int offset , int length )
throws IOException
public int read () throws IOException
public int read ( char [] text ) throws IOException
public long skip ( long n ) throws IOException
public boolean ready ()
Search WWH ::




Custom Search