StringReader Class in Java: Reading Text from a String
Many APIs read text from a Reader. When the text is already in a String, such as a template, a test value or a response body, StringReader lets you pass it to that code without creating a file.
This tutorial covers the class and two practical examples.
1. What Is StringReader in Java?
StringReader extends Reader and reads characters from a String. No charset is involved, because the text is already made of characters. It was added in Java 1.1.
2. StringReader Class Hierarchy
Swipe sideways to see the whole diagram.
3. Constructor of StringReader
public StringReader(String s)
4. Commonly Used Methods
| Method | Description |
|---|---|
int read() | Next character, or -1 at the end of the string. |
int read(char[] cbuf, int off, int len) | Copies characters into an array. |
long skip(long n) | Skips characters. |
boolean ready() | Always ready while the reader is open. |
mark(int) / reset() | Supported; markSupported() returns true. |
void close() | Closes the reader; later reads throw IOException. |
5. Example: Finding Placeholders in a Template
An email service checks which placeholders a message template uses before filling them in.
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class PlaceholderFinder {
public static void main(String[] args) throws IOException {
String template = "Hi {name}, your order {orderId} ships on {date}.";
List<String> found = new ArrayList<>();
try (StringReader in = new StringReader(template)) {
StringBuilder current = null;
int c;
while ((c = in.read()) != -1) {
if (c == '{') {
current = new StringBuilder(); // start of a placeholder
} else if (c == '}' && current != null) {
found.add(current.toString()); // end of a placeholder
current = null;
} else if (current != null) {
current.append((char) c);
}
}
}
System.out.println("Placeholders: " + found);
}
}
Placeholders: [name, orderId, date]
How the code works
read()returns the template one character at a time.{starts collecting a name, and}stores it.- The same logic could read a template file, because it only needs a
Reader.
6. Example: Reading a Text Block Line by Line
A delivery app splits a multi-line address into lines for a shipping label. Wrapping a StringReader in a BufferedReader provides readLine().
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class AddressParser {
public static void main(String[] args) throws IOException {
String address = """
Flat 12B, Lakeview Towers
MG Road
Bengaluru 560001
India
"""; // text block (Java 15)
try (BufferedReader lines = new BufferedReader(new StringReader(address))) {
String line;
int number = 1;
while ((line = lines.readLine()) != null) {
System.out.println("Line " + number++ + ": " + line);
}
}
}
}
Line 1: Flat 12B, Lakeview Towers Line 2: MG Road Line 3: Bengaluru 560001 Line 4: India
How the code works
- The address is written as a text block (Java 15), which keeps its line breaks.
BufferedReaderreads theStringReaderline by line.- For simple cases,
String.lines()(Java 11) does the same without a reader.
7. Common Mistakes and Best Practices
- Using
StringReaderwhere aStringmethod is enough. Usesplit()orlines()for simple tasks. - Reading after
close(). It throwsIOException. - Converting the string to bytes first. No charset is needed; read the characters directly.
8. Java Version Improvements and Java 25 Updates
| Java version | Change |
|---|---|
| Java 1.1 | StringReader introduced. |
| Java 15 | Text blocks make multi-line strings easy to write. |
| Java 24 | Reader.of(CharSequence) also reads a String, StringBuilder or other sequence; standard in Java 25. |
9. Summary
StringReaderreads characters from aString.- It lets you pass in-memory text to code that expects a
Reader. - Wrap it in a
BufferedReaderforreadLine(). - Since Java 24,
Reader.of()is a flexible alternative.