Home Java Example Java Io Java IO StringReader
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java IO StringReader

Advertisement
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
Description
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.

    Syntax
    : 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

Example

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

JavaStringReaderExample.java

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";
            try
              {
                  sr = new StringReader(str);
                  int r;
                  while((r= sr.read()) != -1)
                   {
                      System.out.print((char)r);
                   }
               }
             catch(IOException ioe)
               {
                   System.out.println(ioe);
               }
              finally
               {
                  if(sr != null)
                    {
                       try
                         {
                            sr.close();
                         }
                       catch(Exception e)
                         {
                            System.out.println(e);
                         }
                    }
               }
         }
   }  

Output

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

Download Source Code

Advertisements

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

Ask Questions?    Discuss: Java IO StringReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com