23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public OutputStreamWriter(OutputStream out)<br />

public OutputStreamWriter(OutputStream out, String encoding) throws<br />

UnsupportedEncodingException<br />

<strong>Java</strong> I/O<br />

The first constructor assumes that the text in the stream is to be written using the platform's<br />

default encoding. The second constructor specifies an encoding. There's no easy way to<br />

determine which encodings are supported, but the ones listed in Table B.4 in Appendix B, are<br />

supported by most VMs. For example, this code attaches an OutputStreamWriter to<br />

System.out with the default encoding:<br />

OutputStreamWriter osw = new OutputStreamWriter(System.out);<br />

The default encoding is normally ISO Latin-1, except on Macs, where it is MacRoman.<br />

Whatever it is, you can find it in the system property file.encoding:<br />

String defaultEncoding = System.getProperty("file.encoding");<br />

On the other hand, if you want to write a file encoded in ISO 8859-7 (ASCII plus Greek) you<br />

might do this:<br />

FileOutputStream fos = new FileOutputStream("greek.txt");<br />

OutputStreamWriter greekWriter = new OutputStreamWriter(fos, "8859_7");<br />

The write() methods convert characters to bytes according to a specified character encoding<br />

and write those bytes onto the underlying output stream:<br />

public void write(int c) throws <strong>IO</strong>Exception<br />

public void write(char[] text, int offset, int length) throws <strong>IO</strong>Exception<br />

public void write(String s, int offset, int length) throws <strong>IO</strong>Exception<br />

Once the Writer is constructed, writing the characters is easy. For example:<br />

String arete = "\u03B1\u03C1\u03B5\u03C4\u03B7";<br />

greekWriter.write(arete, 0, arete.length());<br />

The String variable arete is the Unicode-escaped encoding of a?et?, the Greek word for<br />

excellence. The second line writes this word in the ISO 8859-7 character set. In this encoding,<br />

these five Unicode characters (10 bytes) become the five bytes 225, 241, 229, 244, 231, which<br />

encode the word a?et? in ISO 8859-7. You don't have to worry about exactly how this<br />

conversion is performed. You just have to construct the writer, write the string, and let <strong>Java</strong><br />

do the grunt work of figuring out which Unicode characters map to which externally encoded<br />

characters.<br />

Unicode is a fairly large character set; most other character sets don't have all the characters in<br />

Unicode. Attempts to write characters that don't exist in a given set instead produce a<br />

substitution character, generally a question mark.<br />

The getEncoding() method returns a string containing the name of the encoding used by this<br />

writer:<br />

public String getEncoding()<br />

362

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!