io

Java read file Example

Photo of Konstantina DimtsaKonstantina DimtsaMay 1st, 2014Last Updated: May 4th, 2014
0 278 2 minutes read

In this example we will show how to open and read a file in Java. There are many ways to do this, but we will show two of them. For this example, we will read the contents of a text file named“test_file.txt”.

1. Read a file using BufferedReader

This is the most commonly-used method in order to read a file in Java.BufferedReader reads text from a character-input stream, buffering the characters for the more efficient reading of characters, arrays, and lines.

Create a java class namedReadFileBufferedReader.java with the following code:

ReadFileBufferedReader.java

package com.javacodegeeks.javaio.readfile;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class ReadFileBufferedReader {private static String filepath = "/home/konstantina/test_file.txt";public static void main(String[] args) {BufferedReader br;String curline;try {br = new BufferedReader(new FileReader(filepath));while ((curline = br.readLine()) != null) {System.out.println(curline);}br.close();} catch (IOException e) {e.printStackTrace();}}}

In this case, we used the classFileReader along withBufferedReader.FileReader needs the file’s name and path so as to open the file. TheBufferedReader class provides a method namedreadLine() so as to read each line of the file. There is a while loop which is executed tillreadLine() reaches the end of the file.

If we run the above code, we will see the contents of the file“test_file.txt”.

  • Output:
This is a test file.Read file exampleprovided byexamples.javacodegeeks.com

2. Read a file using FileInputStream

This is another way of reading a file.FileInputStream reads streams of raw bytes from a file. So, we need to convert those bytes to characters in order to display the contents of the file to the screen.

Create a java class namedReadFileInputStream.java with the following code:

ReadFileInputStream.java

package com.javacodegeeks.javaio.readfile;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class ReadFileInputStream {private static String filepath = "/home/konstantina/test_file.txt";public static void main(String[] args) {try {File file = new File(filepath);FileInputStream in = new FileInputStream(file);System.out.println("Total bytes to read from file : "+ in.available());int i;StringBuffer buf = new StringBuffer("");while ((i = in.read()) != -1) {// convert byte to char and append to StringBufferbuf.append((char) i);}in.close();System.out.println("File contents :");System.out.println(buf);} catch (IOException e) {e.printStackTrace();}}}

If we run the above code, we will have the following results:

  • Output:
Total bytes to read from file : 78File contents :This is a test file.Read file exampleprovided byexamples.javacodegeeks.com

3. Download the source code

You can download the source code of this example from here:ReadFileExample.zip

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Konstantina DimtsaKonstantina DimtsaMay 1st, 2014Last Updated: May 4th, 2014
0 278 2 minutes read
Photo of Konstantina Dimtsa

Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.