Java StringReader Class
In Java, input and output (I/O) operations are a fundamental aspect of handling data. While we are familiar with reading and writing data from files or networks, sometimes it's necessary to work with data stored in memory. The StringReader
class is designed for exactly this scenario. It is part of the java.io
package and allows you to read characters from a string as if it were an input stream.
The StringReader
class in Java is a subclass of Reader
that allows you to read characters from a string. It provides the functionality of reading data from an in-memory string as though it were a character input stream, which is particularly useful when you want to process strings with the same methods used for file or stream reading.
The StringReader
class is simple to use. To create an instance, you pass a String
to its constructor, like this:
StringReader reader = new StringReader("Some string data here.");
Once created, you can read from it using the read()
method, just like you would with any other Reader
class.
The StringReader
class is typically used in scenarios where you need to read data from a string, rather than from a file or external input stream. It’s commonly used for testing, parsing, or processing in-memory data.
Let’s go through an example to see how to use it.
import java.io.StringReader;
import java.io.IOException;
public class StringReaderExample {
public static void main(String[] args) {
String data = "Java StringReader class example.";
StringReader reader = new StringReader(data);
int character;
try {
// Read characters one by one
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the reader
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
StringReader
object using the string "Java StringReader class example."
.read()
method until the end of the string is reached (indicated by -1
).StringReader
to release the resources.
Java StringReader class example.
The StringReader
class provides multiple read()
methods to read characters from the string:
read()
: Reads a single character at a time and returns the character as an integer.read(char[] cbuf)
: Reads a sequence of characters into a specified character array.read(char[] cbuf, int offset, int length)
: Reads up to length
characters into a specified character array starting at a given offset.Let’s explore these methods in more detail.
read(char[] cbuf)
import java.io.StringReader;
import java.io.IOException;
public class StringReaderArrayExample {
public static void main(String[] args) {
String data = "This is an example.";
StringReader reader = new StringReader(data);
char[] buffer = new char[10]; // Create a buffer to read characters
try {
// Read data into the buffer
int length;
while ((length = reader.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, length)); // Convert the buffer into a string and print
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
StringReader
to read the string "This is an example."
.char[]
buffer of size 10 to read chunks of characters.read()
method reads up to 10 characters into the buffer, and we print out the buffer contents each time it is filled.
This is an
example.
The StringReader
class supports the mark()
and reset()
methods, which allow you to mark a position in the stream and later reset back to that position. This can be helpful for re-reading a portion of the string.
mark()
and reset()
import java.io.StringReader;
import java.io.IOException;
public class StringReaderMarkExample {
public static void main(String[] args) {
String data = "Welcome to StringReader in Java.";
StringReader reader = new StringReader(data);
try {
// Mark position after the 7th character
reader.mark(10);
// Read first 7 characters
for (int i = 0; i < 7; i++) {
System.out.print((char) reader.read());
}
System.out.println("\nResetting reader to the marked position...");
// Reset the reader to the marked position
reader.reset();
// Read the next 7 characters
for (int i = 0; i < 7; i++) {
System.out.print((char) reader.read());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
StringReader
after the 7th character using the mark()
method.reset()
.
Welcome
Resetting reader to the marked position...
Welcome
Both StringReader
and StringBufferReader
are used to read strings in Java, but there is a key difference:
StringReader
: It is designed to read data from a String
. Once the StringReader
is created, the data is immutable (it cannot be modified).StringBufferReader
: It is used to read from a StringBuffer
, which is mutable and can be modified during the read operation.In most cases, StringReader
is sufficient when working with immutable strings.
StringReader
is ideal for scenarios where data is stored in memory as a string and needs to be processed or parsed.StringReader
after use to ensure resources are released.char[]
buffers) to improve efficiency.mark()
and reset()
methods if you need to backtrack and re-read portions of the string.