Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
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
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment