Character streams in Java are designed to handle the reading and writing of text data, which is represented in Unicode. Unlike byte streams, which deal with raw binary data, character streams are specifically meant for text-based input and output. They make it easier to work with characters, strings, and text files.
Character streams belong to the java.io package and include classes and interfaces for handling text-based I/O.
Core Features of Character Streams
- Text-Oriented:
- They are used to read and write text data (characters) rather than raw binary data.
- Unicode support ensures proper handling of international text.
- Two-Way Processing:
- Provides both input and output capabilities.
- Buffering Support:
- Some character streams support buffering, improving performance.
- Automatic Encoding and Decoding:
- Handles character encoding/decoding, converting between bytes and characters automatically.
Key Classes in Character Streams
Stream Type | Input Class | Output Class |
Basic Streams | Reader | Writer |
File Streams | FileReader | FileWriter |
Buffered Streams | BufferedReader | BufferedWriter |
CharArray Streams | CharArrayReader | CharArrayWriter |
String Streams | StringReader | StringWriter |
Filtered Streams | FilterReader | FilterWriter |
Input/Output Streams | InputStreamReader | OutputStreamWriter |
Piped Streams | PipedReader | PipedWriter |
Print Streams | – | PrintWriter |
Key Abstract Classes
1. Reader
- The abstract base class for all character input streams.
- Provides methods to read characters, arrays, or lines of text.
2. Writer
- The abstract base class for all character output streams.
- Provides methods to write characters, arrays, or strings.
Important Methods
Common Methods in Reader
Method | Description |
int read() | Reads a single character and returns it. Returns -1 if the end of the stream is reached. |
int read(char[] cbuf) | Reads characters into an array. |
void close() | Closes the stream and releases system resources. |
Common Methods in Writer
Method | Description |
void write(int c) | Writes a single character. |
void write(char[] cbuf) | Writes characters from an array. |
void write(String str) | Writes a string. |
void close() | Closes the stream and releases system resources. |
Types of Character Streams
1. File Character Streams
- FileReader: Reads character data from a file.
- FileWriter: Writes character data to a file.
Example:
import java.io.*;
public class FileCharacterStreamExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter(“example.txt”);
FileReader reader = new FileReader(“example.txt”)) {
// Writing data to the file
writer.write(“Hello, Character Streams!”);
writer.flush();
// Reading data from the file
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Buffered Character Streams
- BufferedReader: Reads text from a character-input stream and buffers characters for efficient reading.
- BufferedWriter: Writes text to a character-output stream with buffering for efficiency.
Example:
import java.io.*;
public class BufferedCharacterStreamExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(“example.txt”));
BufferedReader reader = new BufferedReader(new FileReader(“example.txt”))) {
// Writing data
writer.write(“Buffered Character Streams are efficient!”);
writer.newLine();
writer.write(“This is a second line.”);
writer.flush();
// Reading data
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. String Streams
- StringReader: Reads characters from a string.
- StringWriter: Writes characters to a string buffer, which can then be converted to a string.
Example:
import java.io.*;
public class StringStreamExample {
public static void main(String[] args) {
try {
StringWriter writer = new StringWriter();
writer.write(“StringWriter Example!”);
// Convert to string
String data = writer.toString();
System.out.println(“Written Data: ” + data);
StringReader reader = new StringReader(data);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. InputStreamReader and OutputStreamWriter
- These classes act as bridges between byte streams and character streams.
- They convert bytes to characters and vice versa using specified character encoding.
Example:
import java.io.*;
public class InputStreamReaderExample {
public static void main(String[] args) {
try (InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isr)) {
System.out.println(“Enter some text: “);
String input = reader.readLine();
System.out.println(“You entered: ” + input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Advantages of Character Streams
- Unicode Support:
- They are ideal for handling international text.
- Ease of Use:
- Simplifies reading/writing of text data compared to byte streams.
- Encoding/Decoding:
- Automatically handles character encoding and decoding.
- Efficient Buffering:
- Buffered streams improve performance by reducing the number of I/O operations.
Disadvantages of Character Streams
- Text Only:
- Cannot handle binary data like images or audio files.
- Encoding Issues:
- Misconfigured encoding can lead to data corruption.
Use Cases
- Reading/Writing Text Files:
- Logs, configuration files, or general text processing.
- Text-Based User Input:
- Accepting input from users in console-based applications.
- String Manipulation:
- Efficiently read and write strings with StringReader and StringWriter.
Conclusion
Character streams in Java simplify handling text data, offering functionality tailored for character-based I/O. By abstracting away the complexities of encoding and decoding, they enable developers to focus on processing text effectively. For applications dealing with text files or string-based data, character streams are an essential tool in Java’s I/O framework.