Arvutiteaduse instituut
  1. Kursused
  2. 2019/20 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2019/20 kevad

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 9

Character streams

Character Stream deals with Unicode characters rather than bytes. Sometimes 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");
char[] mybuffer = new char[1024];
int myRead = myTextIn.read(mybuffer, 0, 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 noticed that the method write of the class OutputStreamWriter 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, it 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 :)

Chapter 9
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused