The perfect place for easy learning...

Java Programming

×

Topics List


Byte Stream in java





In java, the byte stream is an 8 bits carrier. The byte stream in java allows us to transmit 8 bits of data.

In Java 1.0 version all IO operations were byte oriented, there was no other stream (character stream).

The java byte stream is defined by two abstract classes, InputStream and OutputStream. The InputStream class used for byte stream based input operations, and the OutputStream class used for byte stream based output operations.

The InputStream and OutputStream classes have several concreate classes to perform various IO operations based on the byte stream.

The following picture shows the classes used for byte stream operations.

Byte stream in java

InputStream class

The InputStream class has defined as an abstract class, and it has the following methods which have implemented by its concrete classes.

S.No. Method with Description
1 int available()

It returns the number of bytes that can be read from the input stream.

2 int read()

It reads the next byte from the input stream.

3 int read(byte[] b)

It reads a chunk of bytes from the input stream and store them in its byte array, b.

4 void close()

It closes the input stream and also frees any resources connected with this input stream.

OutputStream class

The OutputStream class has defined as an abstract class, and it has the following methods which have implemented by its concrete classes.

S.No. Method with Description
1 void write(int n)

It writes byte(contained in an int) to the output stream.

2 void write(byte[] b)

It writes a whole byte array(b) to the output stream.

3 void flush()

It flushes the output steam by forcing out buffered bytes to be written out.

4 void close()

It closes the output stream and also frees any resources connected with this output stream.

Reading data using BufferedInputStream

We can use the BufferedInputStream class to read data from the console. The BufferedInputStream class use a method read( ) to read a value from the console, or file, or socket.

Let's look at an example code to illustrate reading data using BufferedInputStream.

Example 1 - Reading from console
import java.io.*;

public class ReadingDemo {

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

		BufferedInputStream read = new BufferedInputStream(System.in);
		
		try {
			System.out.print("Enter any character: ");
			char c = (char)read.read();
			System.out.println("You have entered '" + c + "'");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			read.close();
		}
	}

}

When we run the above program, it produce the following output.

InputStream in java

Example 2 - Reading from a file
import java.io.*;

public class ReadingDemo {

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

		FileInputStream fileInputStream = new FileInputStream(new File("C:\\Raja\\dataFile.txt"));
		BufferedInputStream input = new BufferedInputStream(fileInputStream);
		
		try {
			char c = (char)input.read();
			System.out.println("Data read from a file - '" + c + "'");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			input.close();
		}
	}
}

When we run the above program, it produce the following output.

InputStream in java

Writing data using BufferedOutputStream

We can use the BufferedOutputStream class to write data into the console, file, socket. The BufferedOutputStream class use a method write( ) to write data.

Let's look at an example code to illustrate writing data into a file using BufferedOutputStream.

Example - Writing data into a file
import java.io.*;

public class WritingDemo {

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

		String data = "Java tutorials by BTech Smart Class";
		BufferedOutputStream out = null;
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Raja\\dataFile.txt"));
			out = new BufferedOutputStream(fileOutputStream);
			
			out.write(data.getBytes());
			System.out.println("Writing data into a file is success!");
			
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

When we run the above program, it produce the following output.

InputStream in java