Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java FileReader Class
3.
Using BufferedReader Class
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

File Reader

Introduction

We will be heading towards multiple ways of reading and writing a text file. There are several ways to read a text file in Java, e.g., you can use BufferedReader, FileReader, or Scanner class to read a text file. Every class provides something special, e.g., BufferedReader provides buffering of data for fast reading, while Scanner provides parsing ability.

 

Java FileReader Class

 

Java FileReader class is used to read the data from the File. It returns data in byte format like FileInputStream class.

It is a character-oriented class that is used for file handling in Java.

 

Declaration:

public class FileReader extends InputStreamReader  

 

Constructors in this class are as follows:

  1. FileReader(File file): It creates a new FileReader, given the File to read from
  2. FileReader(FileDescriptor fd): It creates a new FileReader, given the FileDescriptor to read from
  3. FileReader(String fileName): It creates a new FileReader, given the name of the File to read from

 

Example:

  

import java.io.FileReader;  
public class Example {  
    public static void main(String args[])throws Exception{    
          FileReader f=new FileReader("D:\\readout.txt");    
          int i;    
          while((i=f.read())!=-1)    
          System.out.print((char)i);    
          f.close();    
    }    
}    
You can also try this code with Online Java Compiler
Run Code

 

Here, we are assuming that you have the following data in the "readout.txt" file:

Hello Coders

 

Output

Hello Coders
You can also try this code with Online Java Compiler
Run Code

Also see, Duck Number in Java

Using BufferedReader Class

BufferedReader class is used to read the text from a character-based input stream. It is mostly used to read data line by line by the readLine() method. It makes the performance of the code much faster. It inherits Reader class.

 

Declaration for Java.io.BufferedReader class:

 

public class BufferedReader extends Reader

 

In this example, we read the data from the text file testout.txt using the Java BufferedReader class.

 

import java.io.*;  
public class Example {  
    public static void main(String args[])throws Exception{    
          FileReader f=new FileReader("D:\\readout.txt");    
          BufferedReader b=new BufferedReader(f);    
  
          int i;    
          while((i=b.read())!=-1){  
          System.out.print((char)i);  
          }  
          b.close();    
          f.close();    
    }    
}    
You can also try this code with Online Java Compiler
Run Code

 

User Input:
Hello Coders
You can also try this code with Online Java Compiler
Run Code

 

Output

Hello Coders
You can also try this code with Online Java Compiler
Run Code


Practice it on online java compiler for better understanding.

FAQs

1. What are the other methods to read a file in Java?

  1. Using File Reader class
  2. Reading the whole File in a List
  3. Read a text file as a String

 

2. How is the Scanner class imported into the java language?

It is imported using the java inbuilt library function: import java. util.Scanner. 

 

3. List some of the constructors of FileReader class.

  1. FileReader(File file)- This constructor creates a new FileReader, given the File to read.
  2. FileReader(FileDescriptor fd)- This constructor creates a new FileReader, given the FileDescriptor to read from.
  3. FileReader(String fileName)- This constructor creates a new FileReader, given the File's name to read from.

 

Key Takeaways

This blog has extensively shown the different ways of reading a text file with examples. Every utility that has been mentioned above has provided something special. 

 

Check out this article to get more information regarding the different methods to take the input. 

 

We hope that this article has helped you enhance your knowledge regarding the File Reader class and if you would like to gain or learn more, check out our articles here. Do upvote our blog to help the other ninjas grow.

 

Happy Learning!!

 

 

Live masterclass