Lecture 21 Streams
Lecture 21 Streams
UNIT IV
Understanding Streams
Streams provide a view of data that lets you specify computations at a
higher conceptual level than with collections. With a stream, you
specify what you want to have done, not how to do it. You leave the
scheduling of operations to the implementation.
For example, suppose you want to compute the average of a certain
property. You specify the source of data and the property, and the
stream library can then optimize the computation, for example by using
multiple threads for computing sums and counts and combining the
results.
Understanding Streams
• Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a
source (keyboard, file, etc.)
• it acts as a buffer between the data source and destination
• Input stream: a stream that provides input to a program
• System.in is an input stream
• Output stream: a stream that accepts output from a program
• System.out is an output stream
• A stream connects a program to an I/O object
• System.out connects a program to the screen
• System.in connects a program to the keyboard
I/O Overview
• I/O = Input/Output
• In this context it is input to and output from programs
• Input can be from keyboard or a file
• Output can be to display (screen) or a file
• Advantages of file I/O
• permanent copy
• output from one program can be input to another
• input can be automated (rather than entered manually)
Types of Stream
• Character Stream - Streams that input and output
characters to files are known as character based
streams, storing data as a sequence of characters
• Byte Stream – Streams that input and output bytes to
files are known as byte-based streams, storing data in
its binary format.
System Class – Java.Lang Package
• System.out refers to the standard output stream. By default, this is
the console.
• System.in refers to standard input, which is the keyboard by default.
• System.err refers to the standard error stream, which also is the
console by default.
Character Stream
Character Stream
Reader Writer
Classes
Stream class Description
BufferedReader Handles buffered input stream.
BufferedWriter Handles buffered output stream.
FileReader Input stream that reads from file.
FileWriter Output stream that writes to file.
InputStreamReader Input stream that translate byte to character
OutputStreamReader Output stream that translate character to byte.
PrintWriter Output Stream that contain print() and println() method.
Reader Abstract class that define character stream input
Writer Abstract class that define character stream output
BufferedReader Class
BufferedReader
• In Java, console input is accomplished by reading from System.in.
• To obtain a character-based stream that is attached to the console, you wrap
System.in in a BufferedReader object, to create a character stream.
• BuffereredReader supports a buffered input stream.
• Constructor : BufferedReader(Reader inputReader)
• inputReader is the stream that is linked to the instance of BufferedReader
that is being created.
• Reader is an abstract class. One of its concrete subclasses is
InputStreamReader, which converts bytes to characters.
• Constructor : InputStreamReader(InputStream inputStream)
• System.in refers to an object of type InputStream
Reading Characters
• To read a character from a BufferedReader, use read( ). The
version of read( ) that we will be using is
• int read( ) throws IOException
• Each time that read( ) is called, it reads a character from the
input stream and returns it as an integer value.
• It returns –1 when the end of the stream is encountered.
• it can throw an IOException.
Example
// Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[])
throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
Reading Strings