Java - FilterReader read() method



Description

The Java FilterReader read() method reads one character at a time and returns it as an integer. It reads one character and returns its ASCII/Unicode integer value. Returns -1 if end of file (EOF) is reached.

Declaration

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

public void 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 - Usage of FilterReader read() method

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

FilterReaderDemo.java

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();
      }
   }
}

Output

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

Example - Reading One Character at a Time Using BufferedReader

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         int data;
         while ((data = fr.read()) != -1) { // Read character by character
            System.out.print((char) data); // Convert integer to character
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Hello

Explanation

  • Uses BufferedReader, a subclass of FilterReader.

  • Reads one character at a time using read().

  • Converts the integer ASCII/Unicode value to a character ((char) data).

  • Stops when read() returns -1 (EOF reached).

Example - Using read() with PushbackReader to Read One Character at a Time

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (FilterReader fr = new PushbackReader(new FileReader("example.txt"))) {
         int data;
         while ((data = fr.read()) != -1) { // Read one character at a time
            System.out.print((char) data); // Convert to character and print
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Java")

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

Java

Explanation

  • Uses PushbackReader, a subclass of FilterReader.

  • Reads one character at a time using read().

  • Converts the integer ASCII/Unicode value to a character and prints it.

  • Stops reading when read() returns -1 (EOF reached).

java_io_filterreader.htm
Advertisements