Skip to content

Instantly share code, notes, and snippets.

@hexawulf
Created May 4, 2025 13:45
Show Gist options
  • Save hexawulf/33759b65e4272ade04a3bf777f8ab3a6 to your computer and use it in GitHub Desktop.
Save hexawulf/33759b65e4272ade04a3bf777f8ab3a6 to your computer and use it in GitHub Desktop.
CompressedFileExample.java - Example for writing and reading compressed text files in Java
import java.io.*;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
public class CompressedFileExample {
public static void main(String[] args) {
// === COMPRESSED WRITING ===
try {
// Step 1: Create a BufferedWriter
// This is the stream the program interacts with directly
BufferedWriter writer = new BufferedWriter(
// Step 2: Convert characters to bytes
new OutputStreamWriter(
// Step 3: Compress the byte stream
new DeflaterOutputStream(
// Step 4: Write bytes to the file
new FileOutputStream(
new File("compressed.txt")))));
// Write 10 lines of "Hello World!" to the compressed file
for (int i = 0; i < 10; ++i) {
writer.write("Hello World!\n");
}
writer.close(); // Always close streams to flush and release resources
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
// === DECOMPRESSED READING ===
try {
// Step 1: Create a BufferedReader
BufferedReader reader = new BufferedReader(
// Step 2: Convert bytes to characters
new InputStreamReader(
// Step 3: Decompress the incoming byte stream
new InflaterInputStream(
// Step 4: Read raw bytes from the file
new FileInputStream(
new File("compressed.txt")))));
// Read and print all lines until null is returned (end of file)
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
@hexawulf
Copy link
Author

hexawulf commented May 4, 2025

πŸ’‘ About This Code

This Java program demonstrates how to store and read compressed text files using a chain of DataStreams (also called a pipeline).


πŸ›  What It Does

  1. Compressed Writing:
    The program creates a file called compressed.txt, writes "Hello World!" 10 times into it, and compresses the output using the DEFLATE algorithm.

  2. Decompressed Reading:
    It then reads that same file, decompresses the data, and prints the original lines back to the console.


🧱 Stream Pipeline Breakdown

For Writing:

BufferedWriter β†’ OutputStreamWriter β†’ DeflaterOutputStream β†’ FileOutputStream β†’ File
BufferedWriter: Used directly by the program to write text

OutputStreamWriter: Converts characters to bytes

DeflaterOutputStream: Compresses the byte stream

FileOutputStream: Writes raw bytes to the file

For Reading:

java
Copy
Edit
BufferedReader β†’ InputStreamReader β†’ InflaterInputStream β†’ FileInputStream β†’ File
BufferedReader: Used by the program to read lines of text

InputStreamReader: Converts bytes back to characters

InflaterInputStream: Decompresses the byte stream

FileInputStream: Reads raw bytes from the file

πŸ“š Key Concepts
The program always interacts with the outermost stream (BufferedReader or BufferedWriter).

The file is always the end target (FileInputStream or FileOutputStream).

You write the stream chain from outermost to innermost in code, but data flows in the opposite direction.

This code is beginner-friendly and aligns with the examples from the DLBCSDSJCL01 course book on Java I/O streams and compression.

yaml
Copy
Edit

---

You can copy and paste this directly into the **GitHub Gist comment** area (as shown in your screenshot), or into your Notion snippet vault for future reference.

Want to add a visual pipeline diagram as well?




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment