
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.