English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java StringReader and its methods with the help of examples.
The StringReader class in the java.io package can be used to read data from a string (character by character).
It inherits from the abstract class Reader.
Note: In StringReader, the specified string acts as the source from which characters are read separately.
To create a StringReader, we must first import the java.io.StringReader package. After importing the package, we can create the string reader.
//create a StringReader StringReader input = new StringReader(String data);
Here, we create a StringReader that reads characters from the specified string named data.
The StringReader class provides implementations for different methods in the Reader class.
read()} - Read a single character from the string reader
read(char[] array) - Read characters from the reader and store them in the specified array
read(char[] array, int start, int length) - Read an equal number of characters from the reader and store them in the specified array starting from the start position
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); //using the read method input.read(array); System.out.println("Reading data from a string:"); System.out.println(array); input.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Reading data from a string: This is the text read from StringReader.
In the above example, we created a string reader named input. The string reader is linked to the string data (data).
String data = "This is a text in the string."; StringReader input = new StringReader(data);
To read data from a string, we use the read() method.
Here, the method reads characters from the reader and stores them in the specified array.
To discard and skip a specified number of characters, you can use the skip() method. For example
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); //using the skip() method input.skip(5); //using the read method input.read(array); System.out.println("Skip ",5the data after the character: "); System.out.println(array); input.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Original Data: This is the text read from the StringReader Skip5characters after data: is the text read from the StringReader
In the above example, we use the skip() method to skip5characters. Therefore, the characters 'T', 'h', 'i', 's', and ' ' are skipped from the original string reader.
To close the string reader, we can use the close() method. After calling the close() method, we will not be able to use the reader to read data from the string.
Method | Description |
---|---|
ready() | Check if the string reader is ready to be read |
mark() | Mark the position of the data that has been read in the marker reader |
reset() | Reset the marker, return to the position set by the reader |