• August 30, 2014
  • Core Java
  • Comments Off on BufferedInputStream and BufferedOutputStream in java

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 javawithease
 */
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 javawithease
 */
class IOTest{
	String str = "Hello www.javawithease.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.

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.

 


Comments are closed.