Buffered streams in Java are used to improve the efficiency of input and output operations by reducing the number of calls to the native API. They achieve this by buffering the input and output data. Buffered streams wrap around existing byte or character streams, providing an additional layer that improves performance.
Table of Contents
- Introduction
- Buffered Stream Classes
- BufferedInputStream
- BufferedOutputStream
- BufferedReader
- BufferedWriter
- Examples
- Buffered Byte Stream Example
- Buffered Character Stream Example
- Conclusion
Introduction
Buffered streams are designed to reduce the number of I/O operations by maintaining an internal buffer. This buffering mechanism minimizes the number of read and write operations by grouping them, which can significantly enhance performance, especially for large files or data streams.
Buffered Stream Classes
BufferedInputStream
BufferedInputStream
adds functionality to another input stream, namely the ability to buffer the input and support the mark
and reset
methods.
Example:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"))) {
int byteData;
while ((byteData = bis.read()) != -1) {
System.out.print((char) byteData); // Cast to char to display the content
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedOutputStream
BufferedOutputStream
adds functionality to another output stream, namely the ability to buffer the output.
Example:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutputStreamExample {
public static void main(String[] args) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
String content = "Hello, World!";
bos.write(content.getBytes()); // Convert string to bytes and write
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader
BufferedReader
reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines.
Example:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line); // Print each line
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedWriter
BufferedWriter
writes text to a character-output stream, buffering characters to provide efficient writing of single characters, arrays, and strings.
Example:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
String content = "Hello, World!";
bw.write(content); // Write the content
bw.newLine(); // Add a new line
} catch (IOException e) {
e.printStackTrace();
}
}
}
Examples
Buffered Byte Stream Example
The following example demonstrates how to use BufferedInputStream
and BufferedOutputStream
to copy a file efficiently.
Example:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedByteStreamExample {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
BufferedInputStream
reads bytes frominput.txt
.BufferedOutputStream
writes bytes tooutput.txt
.- A buffer of 1024 bytes is used to read and write chunks of data, improving performance.
Buffered Character Stream Example
The following example demonstrates how to use BufferedReader
and BufferedWriter
to copy a text file efficiently.
Example:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedCharacterStreamExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
BufferedReader
reads lines of text frominput.txt
.BufferedWriter
writes lines of text tooutput.txt
.- Each line read is written to the output file, followed by a new line.
Conclusion
Buffered streams in Java optimize input and output operations by reducing the number of calls to the native API. This buffering mechanism significantly enhances performance, especially for large files or data streams. Buffered streams can be used with both byte and character streams, making them a versatile tool for efficient I/O operations. By understanding how to use BufferedInputStream
, BufferedOutputStream
, BufferedReader
, and BufferedWriter
, you can efficiently manage and process data in your Java applications.
Comments
Post a Comment
Leave Comment