JavaStringReader

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java StringReader Class

Java StringReader class is a character stream with string as a source. It takes an input string and changes it into character stream. It inherits Reader class.

In StringReader class, system resources like network sockets and files are not used, therefore closing the StringReader is not necessary.

Java StringReader class declaration

Let's see the declaration for Java.io.StringReader class:

  1. public class StringReader extends Reader  

Methods of StringReader class

Method

Description

int read()

It is used to read a single character.

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

It is used to read a character into a portion of an array.

boolean ready()

It is used to tell whether the stream is ready to be read.

boolean markSupported()

It is used to tell whether the stream support mark() operation.

long skip(long ns)

It is used to skip the specified number of character in a stream

void mark(int readAheadLimit)

It is used to mark the mark the present position in a stream.

void reset()

It is used to reset the stream.

void close()

It is used to close the stream.

Java StringReader Example

  1. import java.io.StringReader;  
  2. public class StringReaderExample {  
  3.     public static void main(String[] args) throws Exception {  
  4.         String srg = "Hello Java!!  Welcome to Javatpoint.";  
  5.         StringReader reader = new StringReader(srg);  
  6.         int k=0;  
  7.             while((k=reader.read())!=-1){  
  8.                 System.out.print((char)k);  
  9.             }  
  10.         }  
  11. }  

Output:

Hello Java!! 
Welcome to tutorialtours.