Tuesday, January 28, 2014

I/O - Writers/Readers

If you need to stream characters, you should take advantage of Java’s writer and reader classes, which were designed to support character I/O (they work with char instead of byte). Furthermore, the writer and reader classes take character encoding into account.

Reference: http://tutorials.jenkov.com/

Writer and Reader classes Overview:

The java.io package provides several writer and reader classes that are descendants of the abstract Writer and Reader classes.

Writer classes Hierarchy

Writer is the super class for all writer sub classes.



BufferedWriter, FileWriter and PrintWriter are important classes to remember and are typically used for writing text files.

Reader classes Hierarchy

Reader is the super class for all reader sub classes.



BufferedReader, FileReader are important classes used for reading of text files.

Note: BufferedReader and BufferedWriter achieve better performance.

OutputStreamWriter

This class is a bridge between an incoming sequence of characters and an outgoing stream of bytes.

Characters written to this writer are encoded into bytes according to the default or specified character encoding.

Note The default character encoding is accessible via the file.encoding system property.

Each call to an OutputStreamWriter write() method causes an encoder to be called on the given character(s). 
The resulting bytes are accumulated in a buffer before being written to the underlying output stream. 

Note: The characters passed to the write() methods are not buffered.

Typical constructors are:


  • OutputStreamWriter(OutputStream out) creates a bridge between an incoming sequence of characters and underlying output stream out. The default character encoding is used to encode characters into bytes.
  • OutputStreamWriter(OutputStream out, String charsetName) creates a bridge between an incoming sequence of characters and underlying output stream outcharsetName identifies the character encoding used to encode characters into bytes. This constructor throws java.io.UnsupportedEncodingException when the named character encoding isn’t supported.
The following example uses the second constructor to create a bridge to an underlying file output stream so that Polish text can be written to an ISO/IEC 8859-2-encoded file.

FileOutputStream fos = new FileOutputStream("polish.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "8859_2"); 
char ch = '\u0323'; // Accented N.
osw.write(ch);

InputStreamReader

The class is a bridge between an incoming stream of bytes and an outgoing sequence of characters. Characters read from this reader are decoded from bytes according to the default or specified character encoding.

Each call to an InputStreamReader read() method may cause one or more bytes to be read from the underlying input stream. 
To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

Typical constructors are:
  • InputStreamReader(InputStream in) creates a bridge between underlying input stream in and an outgoing sequence of characters. The default character encoding is used to decode bytes into characters.
  • InputStreamReader(InputStream in, String charsetName) creates a bridge between underlying input stream in and an outgoing sequence of characters. charsetName identifies the character encoding used to decode bytes into characters. This constructor throwsUnsupportedEncodingException when the named character encoding isn’t supported.

The following example uses the second constructor to create a bridge to an underlying file input stream so that Polish text can be read from an ISO/IEC 8859-2-encoded file.

FileInputStream fis = new FileInputStream("polish.txt");
InputStreamReader isr = new InputStreamReader(fis, "8859_2");
char ch = isr.read(ch);

Note OutputStreamWriter and InputStreamReader declare a String getEncoding() method that returns the name of the character encoding in use.

FileWriter

FileWriter is a convenience class for writing characters to files. It subclasses OutputStreamWriter, and its constructors call OutputStreamWriter(OutputStream). An instance of this class is equivalent to the following code fragment:
FileOutputStream fos = new FileOutputStream(pathname);
OutputStreamWriter osw;
osw = new OutputStreamWriter(fos, System.getProperty("file.encoding"));
FileReader
FileReader is a convenience class for reading characters from files. It subclasses InputStreamReader, and its constructors call InputStreamReader(InputStream). An instance of this class is equivalent to the following code fragment:
FileInputStream fis = new FileInputStream(pathname);
InputStreamReader isr;
isr = new InputStreamReader(fis, System.getProperty("file.encoding"));
Performance Improvement
Just like with streams, Reader's and Writer's can be combined into chains to achieve more interesting IO. It works just like combining the Reader with InputStream's or the Writer with OutputStream's. For instance, you can achieve buffering by wrapping a Reader in a BufferedReader, or a Writer in a BufferedWriter. Here are two such examples:
Reader reader = new BufferedReader(new FileReader(...));

Writer writer = new BufferedWriter(new FileWriter(...));

No comments:

Post a Comment