Java-StringReader

StringReader

The StringReader class represents those character streams, whose source is a string. StringReader class extends the Reader class, which is an abstract class for reading data from character streams.

StringReader Constructors

Constructors Description
StringReader(String s) Creates a new string reader.

StringReader Methods

Method Description
int read() Reads a single character.
int read(char[] cbuf, int off, int len) Reads characters into a portion of an array.
boolean ready() Tells whether this stream is ready to be read.
boolean markSupported() Tells whether this stream supports the mark() operation, which it does.
void mark(int readAheadLimit) Sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
long skip(long ns) Skips the specified number of characters in the stream.
void reset() Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
void close() Closes the stream and releases any system resources associated with it.

Program


Program Source

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

public class Javaapp {
    
    public static void main(String[] args) throws IOException {
        
        StringReader sr = new StringReader("India\nTamilnadu\nTirunelveli");
        char ch[] = new char[10];
        sr.skip(6);
        sr.read(ch);
        System.out.print(ch);
        int n;
        while((n=sr.read())!=-1)
        {
            System.out.print((char)n);
        }
        sr.close();
    }
}

Leave a Comment