Java.io.FilterReader.read() Method

Advertisements


Description

The java.io.FilterReader.mark(int readAheadLimit) method reads a single character and return as an integer in the range 0 to 65535.

Declaration

Following is the declaration for java.io.FilterReader.read() method:

public int read()

Parameters

  • NA

Return Value

This method returns the character read, as an integer in the range 0 to 65535, -1 if the end of the stream is reached.

Exception

  • IOException -- If an I/O error occurs.

Example

The following example shows the usage of java.io.FilterReader.read() method.

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      
      FilterReader fr = null;
      Reader r = null;      
      int i=0;
      char c;
      
      try{
         // create new reader
         r = new StringReader("ABCDEF");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // read till the end of the stream
         while((i=fr.read())!=-1)
         {
            // converts integer to character
            c = (char)i;
            
            // print
            System.out.println("Character read: "+c);
         }
      }catch(Exception e){
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally{
         
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

Character read: A
Character read: B
Character read: C
Character read: D
Character read: E
Character read: F



Advertisements
Advertisements