Java - StringReader class



Introduction

The Java StringReader class is a character stream whose source is a string.

Class declaration

Following is the declaration for Java.io.StringReader class −

 public class StringReader extends Reader 

Field

Following are the fields for Java.io.StringReader class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

StringReader(String s)

This creates a new string reader.

Class methods

Sr.No. Method & Description
1 void close()

This method closes the stream and releases any system resources associated with it.

2 void mark(int readAheadLimit)

This method marks the present position in the stream.

3 boolean markSupported()

This method tells whether this stream supports the mark() operation, which it does.

4 int read()

This method reads a single character.

5 int read(char[] cbuf, int off, int len)

This method reads characters into a portion of an array.

6 boolean ready()

This method tells whether this stream is ready to be read.

7 void reset()

This method resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.

8 long skip(long ns)

This method skips the specified number of characters in the stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Reader
  • Java.io.Object

Example - Properly closing a StringReader

The following example shows the usage of StringReader close() method.

StringReaderDemo.java

 package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class StringReaderDemo { public static void main(String[] args) throws IOException { StringReader reader = new StringReader("Hello, world!"); int data = reader.read(); System.out.println("First character: " + (char) data); reader.close(); // closing the reader System.out.println("Reader closed."); } } 

Output

Let us compile and run the above program, this will produce the following result−

 First character: H Reader closed. 

Explanation

  • The reader reads one character successfully.

  • Then it's closed using close().

  • No error occurs because we don't attempt to read after closing.

Example - Basic use of mark() and reset()

The following example shows the usage of StringReader mark(int readAheadLimit) method.

StringReaderDemo.java

 package com.tutorialspoint; import java.io.StringReader; public class StringReaderDemo { public static void main(String[] args) throws Exception { StringReader reader = new StringReader("abcdef"); System.out.print((char) reader.read()); // a reader.mark(3); // mark at position after 'a' System.out.print((char) reader.read()); // b System.out.print((char) reader.read()); // c reader.reset(); // go back to mark (before 'b') System.out.print((char) reader.read()); // b System.out.print((char) reader.read()); // c reader.close(); } } 

Output

Let us compile and run the above program, this will produce the following result−

 abcbc 

Explanation

  • You read 'a', then mark the position.

  • Then read 'b' and 'c'.

  • After reset(), you go back to the position after 'a' and read 'b' and 'c' again.

Example - Checking if mark() is supported

The following example shows the usage of StringReader markSupported() method.

StringReaderDemo.java

 package com.tutorialspoint; import java.io.StringReader; public class StringReaderDemo { public static void main(String[] args) { StringReader reader = new StringReader("Hello, world!"); System.out.println("Mark supported? " + reader.markSupported()); reader.close(); } } 

Output

Let us compile and run the above program, this will produce the following result−

 Mark supported? true 

Explanation

  • This example simply checks if the StringReader supports marking (it does).

  • You can safely use mark() and reset() on this reader.

Advertisements