Java - FileReader read() method



Description

The Java FileReader read() method reads one character at a time and returns its integer ASCII value. When the end of the file is reached, it returns -1.

Declaration

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

 public int read() 

Parameters

NA

Return Value

This method returns the character read, or -1 if the end of the stream has been reached.

Exception

IOException − If an I/O error occurs.

Example - Read a File Character by Character

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

FileReaderDemo.java

 package com.tutorialspoint; import java.io.FileReader; import java.io.IOException; public class FileReaderDemo { public static void main(String[] args) { try (FileReader fileReader = new FileReader("example.txt")) { int character; while ((character = fileReader.read()) != -1) { System.out.print((char) character); // Convert int to char and print } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } } 

Output

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

 Hello, World! 

Explanation

  • Opens example.txt using FileReader.

  • Reads characters one by one until read() returns -1.

  • Converts the integer return value to a char for display.

  • Best for small files but slow for large ones.

Example - Count Number of Characters in a File

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

FileReaderDemo.java

 package com.tutorialspoint; import java.io.FileReader; import java.io.IOException; public class FileReaderDemo { public static void main(String[] args) { try (FileReader fileReader = new FileReader("example.txt")) { int character; int count = 0; while ((character = fileReader.read()) != -1) { count++; } System.out.println("Total number of characters: " + count); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } } 

Output

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

 Total number of characters: 14 

Explanation

  • Reads the file one character at a time.

  • Keeps a count of the total characters in the file.

  • Useful for file analysis (e.g., character statistics).

Example - Read and Convert to Uppercase

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

FileReaderDemo.java

 package com.tutorialspoint; import java.io.FileReader; import java.io.IOException; public class FileReaderDemo { public static void main(String[] args) { try (FileReader fileReader = new FileReader("example.txt")) { int character; while ((character = fileReader.read()) != -1) { System.out.print(Character.toUpperCase((char) character)); // Convert to uppercase } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } } 

Output(Contents of 'example.txt' is 'Hello World!')

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

 HELLO, WORLD! 

Explanation

  • Reads characters one by one.

  • Converts each character to uppercase using Character.toUpperCase().

  • Useful for text processing, like case normalization.

java_io_filereader.htm
Advertisements