Java – Streams

Streams in Java

We would be taking about Java Streams in this lesson. Interestingly, the classes you need to work with streams are all available in the java.io package. A stream represents  movement of data from source to destination. You probably have used streams when you write System.out.println().

 

Now we would cover the following

  1. Introduction to Streams
  2. ByteStreams
  3. Character Streams
  4. Java Standard Streams

 

1. Introduction to Streams

As mention, a stream of flow of data. It can be from source to destination.  It can however be InputStream or OutputStream.

InputStream: Used to read data from source

OutputStream: Used to write date to a destination.

Any other stream fall under either of the two. So let’s consider Byte streams and Character Streams.

 

 

2. Byte Streams

You use byte streams to read or write bytes of data. That is 8-bits of data. Although there are a number of classes that can be used with byte stream, however, the most common are FileInputStream and FileOutputStream.

So let’s look at a program that uses these streams to manage file. The program would copy an input file into an output file

 

import java.io.*;

//Program to copy data 
//from InputStream to OuputStream
public class FilesDemo {

	public static void main(String[] args) throws IOException {
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try {
			File inFile = new File("D:/data/in.txt");
			File outFile = new File("D:/data/out.txt");	
			
			fis = new FileInputStream(inFile);
			fos = new FileOutputStream(outFile);
			
			while(fis.read() != -1) {
				int content = fis.read();
				fos.write(content);
				System.out.println("Writting Something");
			}
			
			System.out.println("Bytes written to output");
			
		} finally {
			fis.close();
			fos.close();
		}
	}
}

Listing 1.0: Program the createa file copy using FileStream

 

Before you run the code, you should already have a file in.txt in the location specified. Then after executing the code, you will have a second file out.txt. This new file have the same content as in in.txt.

 

 

3. Character Streams

Unlike the Byte stream, the Character streams are used to handle input and output of 16-bit unicode characters.

Although there are many classes,  the most common classes used for character streams are, FileReader and FileWriter. You need to know the difference in FileInputStream and FileReader. Also between FileOutputStreand FileWriter

The difference is this:

While FileInputStream reads 1 bye of data at a time, FileReader reads 2 bytes.Same applies to Filewritter. So we do the same thin using FileReader and FileWritter.

 

import java.io.*;

//Program to copy data 
//from FileReader to FileWriter
public class FilesDemo {

	public static void main(String[] args) throws IOException {
		
		FileReader fr = null;
		FileWriter fw = null;
		
		try {
			File inFile = new File("D:/data/in.txt");
			File outFile = new File("D:/data/out.txt");	
			
			fr = new FileReader(inFile);
			fw = new FileWriter(outFile);
			
			while(fr.read() != -1) {
				int content = fr.read();
				fw.write(content);
			}
			
			System.out.println("File written to output");
			
		} finally {
			fr.close();
			fw.close();
		}
	}
}

Listing 1.1: Create a file copy using FileReader and FileWriter

 

 

4. Java Standard Streams

You probably have written a program that requires user to enter input from the keyboard. The program then displays an output to the screen. These are standard streams.

 

Hence, there are three standard streams java provides.

Standard Input

This is the System.in. You use it to get user input. And the keyboards is normally used as the standard input.

 

Standard Output

You use this when you write System.out. Normally, used to write some output for the user to see. The computer display screen is used for this.

 

Standard Error

You use this to output error data that is generated by user program. If an error occurs, information is written using the Standard error stream. It is known as System.err and goes to the  computer screen as well.

For example the program below uses the InputStreamReader. It reads data from Standard Input. Then terminates when the user enters the character ‘q’

 

import java.io.*;

//Program using StandardInputStream 
public class FilesDemo {

	public static void main(String[] args) throws IOException {
		
		InputStreamReader rd = null;
		
		try {
			rd = new InputStreamReader(System.in);
			System.out.println("Enter q to quit...");
			
			char ch;
			do {
				ch = (char)rd.read();
				System.out.println("You entered " + ch);
			}
			while(ch != 'q');
			
			System.out.println("Program terminated");
			
		} finally {
			rd.close();
		}
	}
}

Listing 1.2: Program using InputStreamReader