A file in java can be read by N number of ways. Find below some good ways to read a file in java.
Table of Contents Read file with BufferedReader Read file with try-with-resources Read file with java.nio.file.Files Read file with Apache Commons IOUtils Read file with Guava
Read file with BufferedReader
A most simple, commonly used and efficient method used for long time with BufferedReader.
public void readFileWithBufferedReader(String fileName) { BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader ( new FileReader( fileName ) ); while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { //Do not forget to close the stream if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Read file with try-with-resources
Let’s remove the boiler plate code from above example and use try-with-resources feature of java 7.
public void readFileWithTryWithResources(String fileName) { try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } //Stream is closed automatically catch (IOException e) { e.printStackTrace(); } }
Read file with java.nio.file.Files
This is part of New Java IO API, and make the code easy to read.
public void readFileWithNIO (String fileName) { Path path = Paths.get( fileName ); try ( Stream<String> lines = Files.lines(path) ) { lines.forEach(s -> System.out.println(s)); } catch (IOException ex) { ex.printStackTrace(); } }
Please notice the use oflines.forEachOrdered()
. If you will uselines.forEach()
then you may not get the lines in correct sequence due to asynchronous nature of API.Read file with Apache Commons IOUtils
Apache commons IO library does an excellent job by providing short and easy to use methods for almost all of IO related jobs in java. .e. Reading a file using FileUtils is as easy as below example:
public void readFileWithApacheCommons (String fileName) { try { File file = new File( fileName ); List lines = FileUtils.readLines(file, "UTF-8"); for (String line : lines) { System.out.println( line ); } } catch (IOException ex) { ex.printStackTrace(); } }
Read file with Guava
Guava has similar approach as Apache commons. Only the class name is different – Use Files in place of
FileUtils
.public void readFileWithGuava (String fileName) { try { File file = new File( fileName ); List lines = Files.readLines(file, "UTF-8"); for (String line : lines) { System.out.println( line ); } } catch (IOException ex) { ex.printStackTrace(); } }
Complete Example Sourcecode
package com.howtodoinjava.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadFileExamples { public static void main(String[] args) { final String fileName = "Lorem Ipsum.txt"; readFileWithNIO(fileName); //1 readFileWithTryWithResources(fileName); //2 readFileWithNIO(fileName); //3 readFileWithApacheCommons(fileName); //4 readFileWithGuava(fileName); //5 } public static void readFileWithBufferedReader(String fileName) { BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader ( new FileReader( fileName ) ); while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e.printStackTrace(); } finally { try { //Do not forget to close the stream if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } public static void readFileWithTryWithResources(String fileName) { try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } //Stream is closed automatically catch (IOException e) { e.printStackTrace(); } } public static void readFileWithNIO (String fileName) { Path path = Paths.get( fileName ); try (Stream<String> lines = Files.lines(path)) { lines.forEachOrdered(s -> System.out.println(s)); } catch (IOException ex) { ex.printStackTrace(); } } public static void readFileWithApacheCommons (String fileName) { try { File file = new File( fileName ); List lines = FileUtils.readLines(file, "UTF-8"); for (String line : lines) { System.out.println("line:" + line); } } catch (IOException ex) { ex.printStackTrace(); } } public static void readFileWithGuava (String fileName) { try { File file = new File( fileName ); List lines = Files.readLines(file, "UTF-8"); for (String line : lines) { System.out.println( line ); } } catch (IOException ex) { ex.printStackTrace(); } } }
Let me know if you think any other way to read a file in java – more efficient.
Happy Learning !!
Alejandro Daza
I think one important missing FileChannel