0% found this document useful (0 votes)
12 views19 pages

Lecture 21 Streams

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Lecture 21 Streams

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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

• To read a string from the keyboard, use the version of


readLine( ) that is a member of
• the BufferedReader class. Its general form is shown here:
• String readLine( ) throws IOException
• As you can see, it returns a String object.
Example
// Read a string from console using a BufferedReader.
import java.io.*;
class BRReadLines {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
} }
Excercise
• Create a tiny text editor. Create an array of String objects and then
read in lines of text, storing each line in the array. It should read up to
100 lines or until you enter "stop". Use BufferedReader to read from
the console.
Console Output
• Console output is most easily accomplished with print( ) and println( ),
• These methods are defined by the class PrintStream (which is the type of object
• referenced by System.out).
• System.out is a byte stream, using it for simple program output is still acceptable.
• A character-based alternative is described in the next section.
• PrintStream is an output stream derived from OutputStream,
• It implements the low-level method write( ).
• write( ) can be used to write to the console.
• void write(int byteval)
• This method writes the byte specified by byteval. Although byteval is declared as
an integer, only the low-order eight bits are written.
Example
// Demonstrate System.out.write().
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
PrintWriter Class
• For real-world programs, the recommended method of writing to the console when using Java is through a
PrintWriter stream.
• PrintWriter is one of the character-based classes.
• Using a character-based class for console output makes internationalizing your program easier.
• PrintWriter defines several constructors.
• PrintWriter(OutputStream outputStream, boolean flushingOn)

• outputStream is an object of type OutputStream,


• flushingOn controls whether Java flushes the output stream every time a println( ) method
• If flushingOn is true, flushing automatically takes place. If false, flushing is not automatic.
• PrintWriter supports the print( ) and println( ) methods.
• If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then
display the result.
• To write to the console by using a PrintWriter, specify System.out for the output stream and automatic
flushing. For example, this line of code creates a PrintWriter that is connected to console output:

PrintWriter pw = new PrintWriter(System.out, true);


Example
// Demonstrate PrintWriter
import java.io.*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}

You might also like