Easy learning with example program codes

Byte Streams in java

Byte Streams in java:

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.

Always close the files to avoid serious resource leaks.

Byte streams should only be used for the most primitive I/O. It represents a kind of low-level I/O so you should avoid in case complicated data types like text or graphics.


A ByteArrayInputStream class provides an internal buffer for the bytes read from input stream.

Commonly used constructors of ByteArrayInputStream:

1. ByteArrayInputStream(byte[] buf)

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

2. ByteArrayInputStream(byte[] buf, int offset, int length)

Creates ByteArrayInputStream that uses buf as its buffer array.

Commonly used methods of ByteArrayInputStream:

1. public int read()
This method reads the next byte of data from the InputStream. Returns an int as the next byte of data. If it is end of file then it returns -1.


 * This program is used to read byte array 
 * input using ByteArrayInputStream.
 * @author codesjava
class IOTest{
	String str = "";
	public void readFile(){
		try {
			//Converting string into byte array.
			byte b[] = str.getBytes(); 
			//Creating ByteArrayInputStream object.
			ByteArrayInputStream bais = 
                                new ByteArrayInputStream(b);
			int i;
			//read file.
		} catch (Exception e) {
public class ByteArrayInputStreamExample {
	public static void main(String args[]){
		//creating IOTest object.
		IOTest obj = new IOTest();
		//method call.


Download this example.


ByteArrayOutputStream class uses byte array to write data. The buffer automatically grows as data is written to it.

Commonly used to constructors of ByteArrayOutputStream:

1. ByteArrayOutputStream ()

Creates a new byte array output stream.

2. ByteArrayOutputStream (int size)

Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Commonly used methods of ByteArrayOutputStream:

1. public void writeTo(OutputStream outSt)
Writes the entire content of this Stream to the specified stream argument.


 * This program is used to write data into 
 * a file using ByteArrayOutputStream.
 * @author codesjava
class IOTest{
	String str = "Hello";
	public void writeFile(){
		try {
		  //Creating FileOutputStream object.
		  //It will create a new file before writing if not exist.
		  FileOutputStream fos = 
                       new FileOutputStream("F:\\New folder\\data5.txt");
		  //Creating ByteArrayOutputStream object.
		  ByteArrayOutputStream baos=new ByteArrayOutputStream();
		  //Close file after write operation.
		  System.out.println("Contents written successfully.");
		} catch (Exception e) {
public class ByteArrayOutputStreamExample {
	public static void main(String args[]){
		//Creating IOTest object.
		IOTest obj = new IOTest();
		//method call



Download this example.
Next Topic: DataInputStream and DataOutputStream in java with example.
Previous Topic: FileInputStream and FileOutputStream in java with example.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.


Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki