How to open a file using Java FileReader

By | January 16, 2021
How to open a file using Java FileReader

Introduction

Both FileReader and BufferedReader are used to read data from a text file and display them. We will see how to open a file and then read text from it using simple Java methods. Be sure to be familiar with the Java while loop and for loop to appreciate the below examples fully.

How to open a file using Java FileReader

We will take an ArrayList, which will store the data after reading it from the file using the FileReader class. To read the data, we must know how to open the file. We should also remember to close the file once we are done reading it. In the below example, we will also handle the appropriate exceptions.

Vamware
try {
// We are passing the file location as a String here
String filename = "C:\\Users\\ Desktop\\myfile.txt";
FileReader freader = new FileReader(filename);
// We can also use the File object
File file = new File("C:\\Users\\Desktop\\myfile.txt");
// in which case, we will create FileReader object as:
FileReader freader = new FileReader(file);

The next set of code is same whether you create the FileReader object using String or File object:

// Let's read the file contents character by character using the read() method once
char[] charArr = new char[2048];
freader.read(charArr);
for(char ch : charArr)
System.out.print(ch);
freader.close();
} 
catch (FileNotFoundException e) 
{
System.out.println("Sorry, can't find the file!");
} 
catch (IOException e) 
{
System.out.println("Oops.. something went wrong!");
}

The above method is suitable for small files as we are giving the size of the character array as 2048! To read a file line by line, we have to use the BufferedReader.

How to open a file using Java BufferedReader

BufferedReader has a method readLine() to read an entire line at once instead of reading a file character by character. BufferedReader also uses FileReader as the argument.

BufferedReader bReader = new BufferedReader(freader);
// Reads the first line and moves the cursor to the next line
String text = bReader.readLine();
while(text != null){
// Prints the current line
System.out.println(text);
// Now text stores the next line, if it is null, the loop will exit iteration
text = bReader.readLine();
}
bReader.close();

BufferedReader also has methods to read by character, but to read lines is easier and faster for files with more content. Also, here we have handled IOException and FileNotFoundException, which have to be handled, else the code will not compile. If you are using an IDE, you will get a compile-time error, and Eclipse will add these for you.

Conclusion

We have seen that both FileReader and BufferedReader can be used to open a file in Java. To open, we have to create the required objects. The constructor of the BufferedReader class takes FileReader as the parameter. You can also add another parameter to add the size of the buffer.

Leave a Reply

Your email address will not be published. Required fields are marked *