Buffered Streams
Learn about buffered streams: BufferedInputStream and BufferedOutputStream for efficient byte I/O.
Buffered Streams is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Buffered streams speed up file I/O. They group many small reads/writes into larger operations.
BufferedInputStream concept
BufferedInputStream wraps InputStream and reads chunks of bytes into memory. Reading becomes faster for many small calls.
Code: BufferedInputStream
This reads bytes from a file with buffering. Output shows byte values separated by spaces.
public class Main {
public static void main(String[] args) {
java.nio.file.Path p = java.nio.file.Paths.get("data.bin");
try (java.io.BufferedInputStream bis = new java.io.BufferedInputStream(java.nio.file.Files.newInputStream(p))) {
int b;
while ((b = bis.read()) != -1) {
System.out.print(b + " ");
}
} catch (java.io.IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
BufferedOutputStream concept
BufferedOutputStream collects bytes in memory before writing. This reduces the number of disk writes.
Code: BufferedOutputStream
This writes five bytes to out.bin. BufferedOutputStream flushes in chunks for efficiency.
public class Main {
public static void main(String[] args) {
java.nio.file.Path p = java.nio.file.Paths.get("out.bin");
try (java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(java.nio.file.Files.newOutputStream(p))) {
for (int i = 0; i < 5; i++) {
bos.write(i); // write 5 bytes
}
} catch (java.io.IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Code: copy with buffered streams
Buffered streams make file copy simple and efficient. The file is read and written using byte buffers.
public class Main {
public static void main(String[] args) {
java.nio.file.Path in = java.nio.file.Paths.get("data.bin");
java.nio.file.Path out = java.nio.file.Paths.get("copy.bin");
try (java.io.BufferedInputStream bis = new java.io.BufferedInputStream(java.nio.file.Files.newInputStream(in));
java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(java.nio.file.Files.newOutputStream(out))) {
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
} catch (java.io.IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Buffered streams check
Quick check: Why use BufferedInputStream or BufferedOutputStream?
Recap
Recap: Buffered streams improve performance by grouping small reads/writes into larger operations.
Frequently asked questions
Is the “Buffered Streams” lesson free?
Yes — the full text of “Buffered Streams” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Buffered Streams”?
Learn about buffered streams: BufferedInputStream and BufferedOutputStream for efficient byte I/O. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Buffered Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- java.nio.file Path/Files
- Reading/Writing Text Files
- Buffered Streams