BufferedInputStream and BufferedOutputStream in java

BufferedInputStream and BufferedOutputStream used an internal buffer to store date for read and write operations.

Commonly used constructors of BufferedInputStream:

1. BufferedInputStream(InputStream in)

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

2. BufferedInputStream(InputStream in, int size)

Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Example:

BufferedInputStreamExample.java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
 
/**
 * This program is used to read a 
 * file using BufferedInputStream.
 * @author w3schools
 */
class IOTest{
	public void readFile(){
		try {
			//creating FileInputStream object.
			FileInputStream fis = 
                         new FileInputStream("F:\\New folder\\data1.txt");
 
			//Creating BufferedInputStream object.
			BufferedInputStream bis = 
                                   new BufferedInputStream(fis);
			int i;
			//read file.
			while((i=bis.read())!=-1){
				System.out.print((char)i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class BufferedInputStreamExample {
	public static void main(String args[]){
		//creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call.
		obj.readFile();
	}
}

Output:

Hello.
This is a text file.

Download this example.

Commonly used constructors of BufferedOutputStream:

1. BufferedOutputStream(OutputStream out)

Creates a new buffered output stream to write data to the specified underlying output stream.

2. BufferedOutputStream(OutputStream out, int size)

Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Example:

BufferedOutputStreamExample.java

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
 
/**
 * This program is used to write data into 
 * a file using BufferedOutputStream.
 * @author w3schools
 */
class IOTest{
	String str = "Hello www.w3schools.com";
 
	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\\data3.txt");
 
			//Creating BufferedOutputStream object.
			BufferedOutputStream bos = 
                                  new BufferedOutputStream(fos);
 
			byte b[]=str.getBytes();  
			bos.write(b);
			bos.flush();
		       //Close file after write operation.
			bos.close();  
 
		    System.out.println("Contents written successfully.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class BufferedOutputStreamExample {
	public static void main(String args[]){
		//Creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call
		obj.writeFile();
	}
}

Output:

Contents written successfully.

Download this example.
 
Next Topic: FileReader and FileWriter in java with example.
Previous Topic: DataInputStream and DataOutputStream in java with example.