Big Data and Hadoop Training @$80 or Rs. 6,000 only - valid till Oct 17th Apply Now!

How to Read a File in Java


How to Read a File in Java

In this section we are going to know, How to read a file in Java. We have to follow three step to read a File.

In this section we are going to know, How to read a file in Java. We have to follow three step to read a File.

How to Read a File in Java?

In this section we are going to know, How to read a file in Java. We have to follow three step to read a File.

  • First get the File Object.
  • Create a File Stream from File Object.
  • Use these File Stream to read the Content of file .

Read A File: Reading a file is nothing but reading a character line by line from a file. Now we are going to read a file in that we are using three classes File, FileReader and BufferedReader.

File Class: Java File Class represent the File and Directory pathname in a abstract manner. This class is used to create a File and directories, file searching, file deletion etc. The file object represent the actual file or directory on the disk. To create a new File instance. File object is used to obtain or manipulate the information associated with a disk file, such
as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies. The following constructors can be used to create File objects:.

File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File(URI uriObj) 

Here, directoryPath is the path name of the file, file name is the name of the file or subdirectory, dirObj is a File object that specifies a directory, and uriObj is a URI object that describes.

FileReader Class: This class inherit from the InputStreamReader class. This class is used for reading stream of character. The FileReader class creates a Reader that you can use to read the contents of a file. Its two most commonly used constructors are shown here.

FileReader(String filePath)
FileReader(File fileObj)
Either can throw a FileNotFoundException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file.

BufferedReader class : The class allow you to read a class from character InputStream ,buffering character so as to provide efficient reading of character, array and lines. Following are the points about BufferdReader.

  • The Buffer size may be specified or default size may be used.
  • In general ,each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
  • BufferedReader br=new BufferedReader(new FileReader("file.txt"));

    will buffer the input from specified file. without buffering each invocation of read() or readLine() could cause byte to be read from the file, converted into character, and then returned, which can be very inefficient.


    Here is the program to read a file in Java.

    public class FileRead {
    	public static void main(String args[]) {
    		try {
    			File file = new File("Demo.txt");      // create a 'file' object
    			System.out.println(file.exists());     // check whether file is present or not , It return true or false.
    			FileReader fr = new FileReader(file);  // Get data from this file
    							       // using a file reader.
    			BufferedReader br = new BufferedReader(fr); // To store the contents
    						               // read via File Reader
    			String data =""; // Read br and store a line in 'data' and print data
    			while ((data=br.readLine()) != null) {				
    		} catch (IOException e) {
    			System.out.println(e+"sorry!");       //catch the Exception if any 

    In the above example first we are creating a file object, and after that exist() method return true if the file exist or else false. FileReader class is used to get the content of file and BufferedReader class store the content, With while loop we are reading the character line by line from a file till it become EOF and printing the content of file.


    After compiling and executing the above example output will be as follows:

    Download Source Code