-
Notifications
You must be signed in to change notification settings - Fork 8
Java I O Buffered Streams
In unbuffered I/O, each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
There are four buffered stream classes used to wrap unbuffered streams:
- BufferedInputStream and BufferedOutputStream - create buffered byte streams
- BufferedReader and BufferedWriter - create buffered character streams
Let's demonstrate Buffered Streams I/O classes with examples.
Java BufferedOutputStream class is used for buffering an output stream. It internally uses buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.
package com.javaguides.javaio.fileoperations.examples;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BufferedOutputStreamExample {
public static void main(String[] args) {
File file = new File("C:/Project_Work/workspace/java-io-guide/sample.txt");
String content = "This is the text content";
try (OutputStream out = new FileOutputStream(file);
BufferedOutputStream bout = new BufferedOutputStream(out);) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
bout.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java BufferedInputStream class is used to read information from stream. It internally uses buffer mechanism to make the performance fast.
The important points about BufferedInputStream are:
- When the bytes from the stream are skipped or read, the internal buffer automatically refilled from the contained input stream, many bytes at a time.
- When a BufferedInputStream is created, an internal buffer array is created.
package com.javaguides.javaio.fileoperations.examples;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String[] args) {
try( FileInputStream fin=new FileInputStream("D:\\testout.txt");
BufferedInputStream bin=new BufferedInputStream(fin); ){
int i;
while((i=bin.read())!=-1){
System.out.print((char)i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It inherits Writer class. The buffering characters are used for providing the efficient writing of single arrays, characters, and strings.
Let's see the simple example of writing the data to a text file testout.txt using Java BufferedWriter.
package com.javaguides.javaio.fileoperations.examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("D:\\sample.txt");
BufferedWriter buffer = new BufferedWriter(writer);) {
buffer.write("Welcome to JavaGuides.");
System.out.println("Success");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine() method. It makes the performance fast. It inherits Reader class.
package com.javaguides.javaio.fileoperations.examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (FileReader fr = new FileReader("C:/Project_Work/workspace/java-io-guide/sample.txt");
BufferedReader br = new BufferedReader(fr);) {
int i;
while ((i = br.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}