How to Read a File Using Java 


               


How to Read a File Using Java

Java


In this article, we show how to read the contents of a file using Java.

With Java, we can read different kind of file format in any directory on a computer.

In Java, a file can be read by creating an object of the BufferedReader class and then using the readLine() function to read in each line of the file.

The code to create a read the contents of a file in Java is shown below.



So in the file above we create a class called Readfile.

We then create our main method(), which is the point where execution of our program begins.

We create a String variable named line. This variable will be used later to read the lines from the file we are reading.

In the main method, we create a file object, f, and pass the parameter named "filetoread.txt" into the this File object.

So at this point in the code, the File object f has the value of filetoread.txt.

We then use a try function to try a portion of code. If it doesn't work, exception handling is done.

To read a file, you have to create a BufferedReader object. We create a BufferedReader object named in.

We then take our variable line and set it equal to the BufferedReader object, in, followed by dot and the readLine() function.

This reads in each line of the BufferedReader object into the variable line.

While the line is not equal to null, we print out the line through the System.out.println() function and keep reading in additional lines.

What this code does is it makes it creates a while loop that keeps reading in the lines of a file until the line is equal to null, which means we've reached the end of the file. At this point, we exit the while loop and our program is done.

When dealing with the bufferedReader objects in Java and reading the lines of a bufferedReader object, you need to perform exception handling. This is because there are many reasons why errors may occur. For example, the file you may want to read may not exist. The pathway to the file may be wrong or does not exist, etc. For this reason, Java looks for exception handling when dealing with bufferedReader objects. We provide this through try and catch functions.

Since we didn't specify the path of the file, the Java program creates the new file in the current directory, the one where this Java file saves to.

If you want to specify a directory other than the current directory, then you will have to specify the complete path to the file.

Since this differs according to what operating system you are using, this is the only part of Java that becomes platform dependent.

In a Windows computer, you normally specify the drive you are using with a letter such as the C drive and then specify the path from there to the file that you want to read.

So if you have a file you want to create in the C:\Users\ directory, then you would write this into the string as "C:\\Users\\"

This is because "\" is an escape character; it's used for escaping strings. To show "\" in a string, you would have to put double forward slashes "\\".

So the above code would be modified to the following.



And this is all that is required to read a file using Java.


Related Resources

How to Check if a File Exists Using Java

How to Check if an Object is a File Using Java

How to Check if a File is a Directory Using Java

How to Create a New File Using Java

How to Rename a File Using Java

How to Delete a File Using Java

How to Create a New Directory Using Java

How to List All Files in a Directory Using Java

How to Write Data to a File Using Java

How to Get the Size of a File Using Java




HTML Comment Box is loading comments...