Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

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

Stream buffering

Reading and writing the data is a relatively slow process. Sometimes it is not efficient to read data in small portions. Java has some more classes for reading/writing the data in large portions - BufferedInputStream and BufferedOutputStream:

InputStream sisse = new BufferedInputStream(new FileInputStream("myPicture.jpg"));
OutputStream välja = new BufferedOutputStream(new FileOutputStream("mySecondPicture.jpg"));

The buffer of BufferedInputStream has a 8KiB-byte array by default, which holds the data from the stream. This helps increase the speed of reading/writing and data processing.

Apart from the buffered byte input stream, Java has a class for reading characters using a buffer - BufferedReader. This class also looks for the lines in the buffered text:

InputStream myBytes = new FileInputStream("myInput.txt");
InputStreamReader myText = new InputStreamReader(myBytes, "UTF-8");
BufferedReader myBuffer = new BufferedReader(myText);

String myLine = myBuffer.readLine();
while (myLine != null) {
  System.out.println("Line: " + myLine);
  myLine = myBuffer.readLine(); // reads the next line; if there is no line, null is returned
}
myBuffer.close();

The first three lines can be shorted as follows:

BufferedReader myBufferbr = new BufferedReader(new InputStreamReader(new FileInputStream("myInput.txt"), "UTF-8"));
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