Java Streams

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, Object, localized characters etc.

A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

Java does provide strong, flexible support for I/O as it relates to files and networks but this tutorial covers very basic functionlity related to streams and I/O. We would see most commonly used example one by one:

Reading Console Input:

Java input console 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. Here is most common syntax to obtain BufferedReader:

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

Once BufferedReader is obtained, we can use read( ) method to reach a character or readLine( ) method to read a string from the console.

Reading Characters from Console:

To read a character from a BufferedReader, we would read( ) method whose sytax is as follows:

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. As you can see, it can throw an IOException.

The following program demonstrates read( ) by reading characters from the console until the user types a “q”:

// Use a BufferedReader to read characters from the console.

 

import java.io.*;

 

public class BRRead {

public static void main(String args[]) throws IOException

{

char c;

// Create a BufferedReader using System.in

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’);

}

}

Here is a sample run:

Enter characters, ‘q’ to quit.

123abcq

1

2

3

a

b

c

q

Reading Strings from Console:

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

The following program demonstrates BufferedReader and the readLine( ) method. The program reads and displays lines of text until you enter the word “end”:

// Read a string from console using a BufferedReader.

import java.io.*;

public 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 ‘end’ to quit.”);

do {

str = br.readLine();

System.out.println(str);

} while(!str.equals(“end”));

}

}

Here is a sample run:

Enter lines of text.

Enter ‘end’ to quit.

This is line one

This is line one

This is line two

This is line two

end

end

Writing Console Output:

Console output is most easily accomplished with print( ) and println( ), described earlier. These methods are defined by the class PrintStream which is the type of the object referenced bySystem.out. Even though System.out is a byte stream, using it for simple program output is still acceptable.

Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s