In this tutorial, we will learn about Java StringReader and its techniques with the help of examples.
The StringReader class of the java.io package can be used to understand data (in characters) from strings.
It extends the abstract class Reader.
Note: In StringReader, the specified string acts as a source from where characters are read individually.
In this article, you will learn-
Make a StringReader
To make a StringReader, we should import the java.io.StringReader package first. When we import the package here is how we can make the string reader.
// Creates a StringReader
StringReader input = new StringReader(String data);
Here, we have made a StringReader that reads characters from the predefined string named data.
Techniques for StringReader
The StringReader class gives usage to various strategies present in the Reader class.
read() Method
- read() – reads a single character from the string reader
- read(char[] array) – reads the characters from the reader and stores in the specified array
- read(char[] array, int start, int length) – reads the number of characters equal to length from the reader and stores in the specified array starting from the position start
Example: Java StringReader
import java.io.StringReader;
public class Main {
public static void main(String[] args) {
String data = "This is the text read from StringReader.";
// Create a character array
char[] array = new char[100];
try {
// Create a StringReader
StringReader input = new StringReader(data);
//Use the read method
input.read(array);
System.out.println("Data read from the string:");
System.out.println(array);
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data read from the string:
This is the text read from StringReader.
In the above example, we have made a string reader named input. The string reader is connected to the string data.
String data = "This is a text in the string.";
StringReader input = new StringReader(data);
To read data from the string, we have used the read() strategy.
Here, the technique reads an array of characters from the reader and stores them in the predefined array.
skip() Method
To dispose of and skip the predetermined number of characters, we can use the skip() technique. For instance,
import java.io.StringReader;
public class Main {
public static void main(String[] args) {
String data = "This is the text read from StringReader";
System.out.println("Original data: " + data);
// Create a character array
char[] array = new char[100];
try {
// Create a StringReader
StringReader input = new StringReader(data);
// Use the skip() method
input.skip(5);
//Use the read method
input.read(array);
System.out.println("Data after skipping 5 characters:");
System.out.println(array);
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Original data: This is the text read from the StringReader
Data after skipping 5 characters:
is the text read from the StringReader
In the above example, we have used the skip() strategy to avoid 5 characters from the string reader. Thus, the characters ‘T’, ‘h’, ‘I’, ‘s’, and ‘ ‘ are skipped from the first string reader.
close() Method
To close the string reader, we can use the close() strategy. When the close() technique is called, we can’t use the reader to read data from the string.
Other Methods of StringReader
Method | Description |
ready() | checks if the string reader is ready to be read |
mark() | marks the position in reader up to which data has been read |
reset() | returns the control to the point in the reader where the mark was set |
To learn more, visit Java StringReader (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.