In this tutorial, we will learn how to read file in Java. 4 easy ways will be used :
Table of contents
1- BufferedReader
Since JDK 1.1
The first easy way to read file is by using the BufferedReader class from the java.io package. Below an example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/*** * Using BufferedReader * * @author Radouane Roufid. * */ public class BufferedReaderTutorial { public static void main(String[] args) { //Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; Reader reader; BufferedReader bufferedReader = null; try { //Opening the file reader = new FileReader(filePath); bufferedReader = new BufferedReader(reader); //Reading the file String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { System.out.println(currentLine); } } catch (FileNotFoundException e) { System.out.println("The file " + filePath + "is not found !"); e.printStackTrace(); } catch (IOException e) { System.out.println("Problem occurs when reading file !"); e.printStackTrace(); } finally { //Closing the file if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { System.out.println("Problem occurs when closing file !"); e.printStackTrace(); } } } } } |
Make sure you close the file in the end of the program, otherwise it will be locked and not readable by other process.
2- Scanner
Since JDK 1.5
The second way is to use Scanner from the java.util package. Useful if you want to parse lines and break it into tokens using patterns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; Scanner scanner = null; File file = new File(filePath); try { //Opening the file scanner = new Scanner(file); //Reading the file while(scanner.hasNext()) { String currentLine = scanner.nextLine(); System.out.println(currentLine); } } catch (FileNotFoundException e) { System.out.println("The file " + filePath + "is not found !"); e.printStackTrace(); } finally { //Closing the file scanner.close(); } } |
3- Try with resources
Since JDK 1.7
The try with resources statement ensures that each resource is closed at the end of the statement. No need to use finally block to ensure the closing of the resource. You can either use it with BufferedReader or Scanner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { //Reading the file String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { System.out.println(currentLine); } } catch (FileNotFoundException e) { System.out.println("The file " + filePath + "is not found !"); e.printStackTrace(); } catch (IOException e) { System.out.println("Problem occurs when reading file !"); e.printStackTrace(); } } |
4- Apache commons IO
Since JDK 1.6 For Commons IO 2.4
Since JDK 1.5 For Commons IO 2.2
You can also use the Apache Commons IO library to read file in Java. To do this, add the following dependency to your pom.xml
1 2 3 4 5 |
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> |
The Apache Commons IO library offers the FileUtils class to deal with files. You can either use the method readLines or LineIterator. We will see both.
-
ReadLines
The readLines method returns a list of strings representing each line in the file. The file is always closed at the end. bellow how to use it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; File file = new File(filePath); try { //Reading the file List<String> lines = FileUtils.readLines(file); for(String line : lines) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } |
-
LineIterator
Iterate over each line of the file. File must be closed in the end of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void main(String[] args) { // // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; File file = new File(filePath); LineIterator lineIterator = null; try { //Reading the file lineIterator = FileUtils.lineIterator(file); while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { //Closing the file. LineIterator.closeQuietly(lineIterator); } } |