Define byte stream and character stream. Write a program to read the contents of file.
Byte Stream vs Character Stream in Java (with Program to Read a File)
Definitions
- Byte Stream: Works with raw 8-bit bytes. It is used for reading and writing binary data such as images, audio, PDFs, or any non-text content. Core abstract classes: InputStream and OutputStream (e.g., FileInputStream, BufferedInputStream).
- Character Stream: Works with 16-bit Unicode characters. It is used for reading and writing text data and can handle character encoding properly. Core abstract classes: Reader and Writer (e.g., BufferedReader, FileReader, InputStreamReader).
Key Differences
- Data type: Byte streams handle bytes; character streams handle characters.
- Use case: Use byte streams for binary files; use character streams for text files.
- Encoding: Byte streams do not deal with encoding; character streams can handle encoding (e.g., UTF-8) via readers/writers.
Program 1: Read Text File Using Character Streams (Recommended for Text)
This program reads a text file line by line using BufferedReader with UTF-8 encoding and prints its contents to the console.
// File: ReadTextFileChar.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
public class ReadTextFileChar {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java ReadTextFileChar <path-to-text-file>");
return;
}
String path = args[0];
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
Program 2: Read File Using Byte Streams (Works for Any File)
This program reads raw bytes using FileInputStream and writes them directly to the console output stream.
// File: ReadFileBytes.java
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileBytes {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java ReadFileBytes <path-to-file>");
return;
}
String path = args[0];
byte[] buffer = new byte[4096];
try (FileInputStream fis = new FileInputStream(path)) {
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
// Write raw bytes to standard output
System.out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
How to Compile and Run
javac ReadTextFileChar.java java ReadTextFileChar input.txt javac ReadFileBytes.java java ReadFileBytes input.txt
Tips for B.Tech CSE Students
- Use character streams for text to avoid encoding issues; specify StandardCharsets.UTF_8 when possible.
- Use byte streams for binary files (images, PDFs); do not convert bytes to String for binary data.
- Prefer try-with-resources to automatically close streams and prevent resource leaks.
