Java Reader


Java Reader

It is an abstract class which is used for reading character streams. read(char[], int, int) and close() are the only methods which a subclass must implement. Many subclasses will override some of the methods to provide higher efficiency, additional functionality, or both. CharArrayReader, FilterReader,  BufferedReader , InputStreamReader, PipedReader, StringReader are few of the examples in Reader class implementation.

Class Declaration

The following is the syntax of class declaration

public class Reader extends Object implements DataOutput, DataInput, Closeable

Field

The Field for java.io.Reader class are

protected Object lock- the stream synchronization is carried out by this object

Constructors in Java BufferedInputStream

Sl No

Constructor

Description

1

protectedReader()

Critical sections will get synchronized on itself of the created new character-stream

2

protectedReader(Object Lock)

Critical sections get synchronized on the given object in the new character – stream created


Class Methods

Sl No

Method Name

1

abstract void close()

2

void mark(int readAheadLimit)

3

int read()

4

boolean markSupported()

5

int read(char[] cbuf)

6

int read(CharBuffer target)

7

abstract int read(char[] cbuf, int off, int len)

8

void reset()


Example :

package com.javatpoint; import java.io.FileReader; public class FileReaderExample { public static void main(String args[])throws Exception{ FileReader fr=new FileReader("knowledge2life.txt"); int i; while((i=fr.read())!=-1) System.out.print((char)i); fr.close(); } }

OUTPUT:

Welcome to Knowledge2life.