Session 10 |
Character streams
Character Stream deals with Unicode characters rather than bytes. Sometime the character sets used locally are different, non-Unicode. Character I/O automatically translates a local character set to Unicode upon I/O operation without extensive intervention of the programmer. Using Character Stream is safe for future upgrades to support Internationalization even though the application may use a local character set such as ASCII. The character stream classes make the transformation possible with very little recoding. Character stream classes are derived from abstract classes called Reader and Writer, e.g. InputStreamReader
and OutputStreamWriter
.
InputStreamReader
is a bridge between a byte stream and a character stream. The class reads bytes and decodes them into characters using a specified charset:
//Define an input byte stream InputStream myInput = new FileInputStream("myFile.txt"); //Read characters using the byte stream InputStreamReader myTextIn = new InputStreamReader(myInput, "UTF-8"); myTextIn.read(1024);
OutputStreamWriter
is a bridge between a character stream to a byte stream - characters are encoded into bytes using a specified charset. An example:
//Define output byte stream OutputStream myCopy = new FileOutputStream("myCopyFile.txt"); //Write characters using the output byte stream OutputStreamWriter myTextOut = new OutputStreamWriter(myCopy, "UTF-8"); myTextOut.write("hello world!");
Most probably you have notices that the method write
of the class OutputStreamWriteri
takes in a string; however, InputStreamReader
cannot read text line by line. The reason is similar to the input byte stream InputStream
: InputStreamReader
does not read the whole file at once, but reads it in portions. This is because InputStreamReader
has to decode bytes into symbols and put them into the buffer, not to look for a new line break. If we need to read some text line by line, we have to use another classes. Check the next page :)
Session 10 |