Java File Reader Example

FileReader is class for reading character files.It present in java.io package.It implements Closeable, AutoCloseable, Readable interfaces.It returns data in byte format like FileInputStream class.

Constructures from FileReader class

FileReader(File file)
Creates a new FileReader, given the File to read from.It opens the given file in read mode. If file doesn't exist, it throws FileNotFoundException.

FileReader(FileDescriptor fd)
Creates a new FileReader, given the FileDescriptor to read from.

FileReader(String fileName)
Creates a new FileReader, given the name of the file to read from.It opens the given file in read mode. If file doesn't exist, it throws FileNotFoundException.

Methods of FileReader class

int read()
It is used to return a character in ASCII form. It returns -1 at the end of file.

void close()
It is used to close the FileReader class.

FileReader Example

package com.javacodestuffs.core.nio.files; import java.io.FileReader; public class FileReaderExample { public static void main(String args[]) throws Exception { FileReader fr = new FileReader("abc.txt"); int i; while ((i = fr.read()) != -1) System.out.print((char) i); fr.close(); System.out.println("File Reading successful."); } } output: "Hello World!!! File Reading successful.

Reading text file as ArrayList

Here abc.txt is

Hello World!!! Welcome to java programming. We are learning java file operations.

FileReaderExample.java

package com.javacodestuffs.core.nio.files; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class FileReaderExample { public static void main(String[] args) throws IOException { ArrayList result = new ArrayList<>(); try (FileReader f = new FileReader("abc.txt")) { StringBuffer sb = new StringBuffer(); while (f.ready()) { char c = (char) f.read(); if (c == '\n') { result.add(sb.toString()); sb = new StringBuffer(); } else { sb.append(c); } } if (sb.length() > 0) { result.add(sb.toString()); } } System.out.println(result); } } output: [Hello World!!!, Welcome to java programming., We are learning java file operations.]

Convert file content to Arraylist Using Files.readAllLines

package com.javacodestuffs.core.nio.files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class FileReadAllLines { public static void main(String[] args) throws IOException { List result = Files.readAllLines(Paths.get("abc.txt")); System.out.println(result); } } [Hello World!!!, Welcome to java programming., We are learning java file operations.]

Reading a File from the Classpath

package com.javacodestuffs.core.nio.files; import java.io.InputStream; public class ReadFileFromClassPath { public static void main(String[] args) { String expectedData = "Hello, world!"; Class clazz = ReadFileFromClassPath.class; InputStream inputStream = clazz.getResourceAsStream("abc.txt"); try { int content; while ((content = inputStream.read()) != -1) { System.out.print((char) content); } } catch (Exception e) { e.printStackTrace(); } } }

Using BufferedReader

BufferedReader uses a char buffer to simultaneously read multiple values from a character-input stream and hence reduces the number of read() calls made by the underlying FileStream.

package com.javacodestuffs.core.nio.files; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class FIleReadUsingBufferedReader { public static void main(String[] args) throws FileNotFoundException, IOException { try (BufferedReader br = new BufferedReader(new FileReader("abc.txt"))) { while (br.ready()) { System.out.print(br.readLine()); } } } } output: Hello World!!!Welcome to java programming.We are learning java file operations.

Using Scanner

Scanner is a simple text scanner, used for parsing primitive types and strings, using regular expressions.

For file read operations using the scanner we can iniatialize sacanner as

Scanner s = new Scanner(new File(fileName)); Scanner s = new Scanner(new FileReader(fileName));

Scanner breaks its input into tokens using a delimiter, default is whitespace. These tokens can be converted into values of different types, by using various next (nextInt, nextLong,nextDouble etc) methods available.

package com.javacodestuffs.core.nio.files; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class FIleReadUsingScanner { public static void main(String[] args) throws FileNotFoundException { try (Scanner s = new Scanner(new FileReader("abc.txt"))) { while (s.hasNext()) { System.out.print(s.nextLine()); //s.nextInt() or s.nextDouble() etc } } } }

Post/Questions related to Java File Reader Examples

Create New File in Java
Read file to byte[] array in Java How to Download File from URL in Java
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen Java file Reader Examples. All source code in the article can be found in the GitHub repository.