The Wayback Machine - https://web.archive.org/web/20200219194019/https://www.geeksforgeeks.org/java-io-reader-class-java/amp/

Java.io.Reader class in Java

It is an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
Constructors:

Methods:

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program demonstrating Reader methods
import java.io.*;
import java.nio.CharBuffer;
import java.util.Arrays;
class ReaderDemo
{
    public static void main(String[] args) throws IOException
    {
        Reader r = new FileReader("file.txt");
        PrintStream out = System.out;
        char c[] = new char[10];
        CharBuffer cf = CharBuffer.wrap(c);
  
        //illustrating markSupported()
        if(r.markSupported()) {
            //illustrating mark()
            r.mark(100);
            out.println("mark method is supported");
        }
        //skipping 5 characters
        r.skip(5);
  
        //checking whether this stream is ready to be read.
        if(r.ready()) 
        {
            //illustrating read(char[] cbuf,int off,int len)
            r.read(c,0,10);
            out.println(Arrays.toString(c));
  
            //illustrating read(CharBuffer target )
            r.read(cf);
            out.println(Arrays.toString(cf.array()));
              
            //illustrating read()
            out.println((char)r.read());
        }
        //closing the stream
        r.close();
    }
}
chevron_right

Output :

[f, g, h, i, g, k, l, m, n, o]
[p, q, r, s, t, u, v, w, x, y]
z

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



Article Tags :
Practice Tags :