Home Java Example Java Io Java IO StringReader

Related Tutorials


Share on Google+Share on Google+

Java IO StringReader

In this section we will discuss about the StringReader class in Java.

Java IO StringReader

In this section we will discuss about the StringReader class in Java.

StringReader class reads the string from the input stream. This class extends the java.io.Reader. Source for this character stream is a string. This class provides a constructor for creating new StringReader and various of methods( either inherited from its super class or of itself) to do work in various ways such as to read string, mark the position etc.

Constructor of StringReader

Constructor Name
StringReader(String s) This constructor is used to create a new StringReader which source is a string.
Syntax : public StringReader(String s)

Methods in StringReader

  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public void close() throws IOException
  • mark(int readAheadLimit) : This method is used to mark the current position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
  • markSupported() : This method returns a boolean value which specifies that whether this stream is supported by mark() method or not.

    Syntax : public boolean markSupported()
  • read() : This method is used to read a character.

    Syntax : public int read() throws IOException
  • read(char[] cbuf, int off, int len) : This method is used to read the specified length of character at the specified position from a specified char array.

    Syntax : public int read(char[] cbuf, int off, int len) throws IOException
  • ready() : This method returns boolean value which specifies whether this stream is ready to be read or not.

    Syntax : public boolean ready() throws IOException
  • reset() : This method is used to reset the marked position to the beginning.

    : public void reset() throws IOException
  • skip(long ns) : This method is used to skip a specified number of characters.

    Syntax : public long skip(long n) throws IOException


An example is being given here which will demonstrate you about how to use the StringReader class in Java. In this example I have created a Java class named JavaStringReaderExample.java where I have created a constant string value which will be further read by the StringReader. Then created a new StringReader to read the string as of its input stream. To read the string I have used the read() method.

Source Code


import java.io.IOException;
import java.io.StringReader;

public class JavaStringReaderExample 
      public static void main(String args[])
            StringReader sr = null;
            String str = "\n Java IO StringReader \n";
                  sr = new StringReader(str);
                  int r;
                  while((r= sr.read()) != -1)
             catch(IOException ioe)
                  if(sr != null)
                       catch(Exception e)


When you will execute the above example you will get the output as follows :

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 15, 2012

Related Tutorials

Discuss: Java IO StringReader  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image