Spread the love

StringReader Class in Java

Spread the love

StringReader class in Java

StringReader class is a character input stream that uses a String object as the source of the characters it returns. When we create a StringReader, we must specify the String that it is to read from. In other words, the StringReader class represents a character stream whose data source is a String.

StringReader defines the Reader methods, and supports mark) and reset (). If reset () is called before mark () has been called, the stream is reset to the beginning of the specified string, StringReader is a character stream analog to StringBufferInputStream(deprecated now).

The class definition of StringReader is given as

public class java.io.StringReader extends java.io.Reader {
//Public Constructor
public StringReader(String s);
// Public Instance Methods
public void close();// Defines Reader
public void mark(int readAheadLimit) throws IOException;// Overrides Reader 
public boolean markSupported()// Overrides Reader
public int read() throws IOException;// Overrides Reader
public int read(char [ ] cbuf, int off, int len) throws IOException;//Defines Reader 
public boolean ready();// Overrides Reader
public void reset() throws IOException;// Overrides Reader
public long skip(long ns) throws IOException;// Overrides Reader
}

The details of the class structure are given as follows:

public StringReader(String s);

public StringReader(String s) constructor creates a StringReader that uses the given String as its data source. The data is not copied, so changes made to the String affect the data that the StringReader returns.

Parameter
s – The String to use.

public void close();

public void close() method closes the reader by removing the link between this StringReader and the String it was created with.

public void mark(int readAheadLimit);

public void mark(int readAheadLimit) method causes the StringReader to remember its current position. A subsequent call to reset() causes the object to return to that saved position, and thus re-read a portion of the string. Because the data for this stream comes from a String, there is no limit on reading ahead, so readAheadLimit is ignored.

READ  Abstract Class DatagramSocketImpl in Java
READ  Class PropertyResourceBundle in Java

Parameter
readAheadLimit – The maximum number of characters that can be read before the saved position becomes invalid.

public boolean markSupported();

public boolean markSupported() method returns true to indicate that this class supports mark() and reset().
This method returns the boolean value true.

public int read();

public int read() method returns the next character from the string. The method cannot block.

This method returns the next character or -1 if the end of the string is encountered.

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

public int read(char[] cbuf, int off, int len) method copies up to len characters from the internal buffer into the given array cbuf, starting at index off.

This method returns the actual number of characters read or -1 if the end of the string is encountered immediately.

Parameter
cbuf – An array of characters to be filled from the stream.
off – An offset into the character array.
len – The number of characters to read.

public boolean ready();

public boolean ready() method returns true, If there is any data left to be read from the string.
This method returns a boolean value that indicates whether the stream is ready to be read.

public void reset();

public void reset() method resets the position of the StringReader to the position that was saved by calling the mark() method. If mark() has not been called, the StringReader is reset to read from the beginning of the string.

public long skip(long n);

public long skip(long n) method skips ns characters of input. If you try to skip past the end of the string, the stream is positioned at the end of the string.

READ  Class Short in Java
READ  Polymorphism Concept Simplified in Java

This method returns the actual number of bytes skipped.
Parameter
n – The number of bytes to skip.
Apart from these StringReader class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

StringReader class also has inherited one method from Reader. That is as follows:

  • read(char[])




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love