VOOZH about

URL: https://www.geeksforgeeks.org/java/java-filereader-class-read-method-with-examples/

⇱ Java FileReader Class read() Method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java FileReader Class read() Method with Examples

Last Updated : 16 Feb, 2022

The read() method of FileReader class in Java is used to read and return a single character in the form of an integer value that contains the character's char value. The character read as an integer in the range of 0 to 65535 is returned by this function. If it returns -1 as an int number, it means that all of the data has been read and that FileReader may be closed.

Syntax:

public abstract int read()

Returns: The read() method returns a single character in the form of an integer value that contains the character's char value. It returns -1 when all of the data has been read and that FileReader may be closed.

Example 1: We are calling the read() method of FileReader class to read the data from the file, this method reads the one character at a time and returns its ASCII value in the integer format. To print the actual data we must typecast it to the char.

The input.txt file has the following content:

👁 Image

Output:

👁 Image

Example 2:

After creating a FileReader, we read each character and report it to the console using the read() function.

Output:

👁 Image
Comment
Article Tags: