In Java, when working with text-based input and output (character streams), the core classes to use are Reader
and Writer
. These are abstract classes provided in the java.io
package that form the foundation for all character-based input and output operations.
java.lang.Object
├── java.io.Reader
│ ├── BufferedReader
│ ├── CharArrayReader
│ ├── FilterReader
│ ├── InputStreamReader
│ │ └── FileReader
│ ├── PipedReader
│ ├── StringReader
│
└── java.io.Writer
├── BufferedWriter
├── CharArrayWriter
├── FilterWriter
├── OutputStreamWriter
│ └── FileWriter
├── PipedWriter
├── PrintWriter
├── StringWriter
Reader
: Abstract class for reading character streams.Writer
: Abstract class for writing character streams.
They are used when working with text data, unlike InputStream
and OutputStream
which work with binary data.
Reads a single character. Returns -1 at the end of the stream.
import java.io.*;
public class ReaderReadExample {
public static void main(String[] args) {
try (Reader reader = new StringReader("Hello")) {
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch); // Prints each character
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation: StringReader
is used here to simulate a stream. It reads characters one by one.
Reads characters into an array and returns the number of characters read.
import java.io.*;
public class ReaderReadArrayExample {
public static void main(String[] args) {
try (Reader reader = new StringReader("Java I/O")) {
char[] buffer = new char[10];
int len = reader.read(buffer);
System.out.println("Read characters: " + new String(buffer, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Read characters: Java I/O
Closes the reader and releases any associated system resources.
import java.io.*;
public class ReaderCloseExample {
public static void main(String[] args) {
try (Reader reader = new StringReader("Close Example")) {
reader.read();
System.out.println("Reader closed automatically using try-with-resources");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reader closed automatically using try-with-resources
Writes a single character.
import java.io.*;
public class WriterWriteCharExample {
public static void main(String[] args) {
try (Writer writer = new FileWriter("output1.txt")) {
writer.write(65); // Writes character 'A'
System.out.println("Single character written.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Single character written.
(File output1.txt
contains: A)
Writes an array of characters.
import java.io.*;
public class WriterWriteCharArrayExample {
public static void main(String[] args) {
char[] data = { 'J', 'a', 'v', 'a' };
try (Writer writer = new FileWriter("output2.txt")) {
writer.write(data);
System.out.println("Character array written.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
(File output2.txt
contains: Java)
Writes an entire string.
import java.io.*;
public class WriterWriteStringExample {
public static void main(String[] args) {
try (Writer writer = new FileWriter("output3.txt")) {
writer.write("Java Writer Example");
System.out.println("String written successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
String written successfully.
(File output3.txt
contains: Java Writer Example)
Flushes the writer, forcing any buffered output to be written.
import java.io.*;
public class WriterFlushExample {
public static void main(String[] args) {
try (Writer writer = new FileWriter("output4.txt")) {
writer.write("Flushed output.");
writer.flush();
System.out.println("Output flushed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Closes the stream and releases system resources.
import java.io.*;
public class WriterCloseExample {
public static void main(String[] args) {
try (Writer writer = new FileWriter("output5.txt")) {
writer.write("Closing stream");
System.out.println("Writer closed using try-with-resources.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writer closed using try-with-resources.
- Use Reader when you want to read text data character by character or in chunks.
- Use Writer when writing character data to a file, memory, or other output destinations.
- For raw byte data like images or audio, prefer
InputStream
and OutputStream
.
In this blog, we learned about the Reader
and Writer
classes, the foundation of Java's character-based I/O system. We explored important methods such as read
, write
, flush
, and close
with clear examples, outputs, and practical usage. These classes pave the way for more advanced classes like BufferedReader
, FileReader
, PrintWriter
, etc., which we’ll cover in upcoming blogs.
Subscribe to our newsletter
Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.