Java How to read a file
By:Roy.LiuLast updated:2019-08-11
In Java, there are few ways to read a file.
1. Java 8 – Files.lines, it will return a Stream
//@Since 1.8 Stream<String> lines = Files.lines(Paths.get("app.log")); List<String> content = lines.collect(Collectors.toList());
2. Java 7 – Files.readAllBytes or Files.readAllLines
//@Since 1.7 // Returns a byte[] byte[] bytes = Files.readAllBytes(Paths.get("app.log")); String content = new String(bytes); // Returns a List String List<String> content = Files.readAllLines(Paths.get("app.log"));
3. And this classic BufferedReader
try (FileReader reader = new FileReader("app.log"); BufferedReader br = new BufferedReader(reader)) { // read line by line String line; while ((line = br.readLine()) != null) { System.out.println(line); } catch (IOException e) { //e
1. Files.lines
In Java 8, we can use Files.lines to read a file into a Stream, it will close the resources (opened file) automatically.
app.log
Line 1 Line 2 Line 3 Line 4 Line 5
@FileExample1.java
package com.mkyong; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class FileExample1 { public static void main(String[] args) { try { Stream<String> lines = Files.lines(Paths.get("app.log")); List<String> content = lines.collect(Collectors.toList()); content.forEach(x -> System.out.println(x)); } catch (IOException e) { System.err.format("IOException: %s%n", e);
Terminal
Line 1 Line 2 Line 3 Line 4 Line 5
2. Files.readAllBytes
In Java 7, we can use Files.readAllBytes or Files.readAllLines to read a file, it will close the resources (opened file) automatically.
FileExample2.java
package com.mkyong; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FileExample2 { public static void main(String[] args) { try { byte[] bytes = Files.readAllBytes(Paths.get("app.log")); String content = new String(bytes); System.out.println(content); } catch (IOException e) { System.err.format("IOException: %s%n", e);
Output
Terminal
Line 1 Line 2 Line 3 Line 4 Line 5
3. BufferedReader
3.1 A classic BufferedReader with try-with-resources to auto close the resources.
FileExample3.java
package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileExample3 { public static void main(String[] args) { try (FileReader reader = new FileReader("app.log"); BufferedReader br = new BufferedReader(reader)) { // read line by line String line; while ((line = br.readLine()) != null) { System.out.println(line); } catch (IOException e) { System.err.format("IOException: %s%n", e);
3.2 In the old days, we have to close everything manually.
FileExample3.java
package com.mkyong.calculator; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileExample3 { public static void main(String[] args) { BufferedReader br = null; FileReader fr = null; try { fr = new FileReader("app.log"); br = new BufferedReader(fr); // read line by line String line; while ((line = br.readLine()) != null) { System.out.println(line); } catch (IOException e) { System.err.format("IOException: %s%n", e); } finally { try { if (br != null) br.close(); if (fr != null) fr.close(); } catch (IOException ex) { System.err.format("IOException: %s%n", ex);
4. Scanner
Let end this article with the classic Scanner example.
FileExample4.java
package com.mkyong; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class FileExample4 { public static void main(String[] args) { try (FileReader fr = new FileReader("app.log"); Scanner scanner = new Scanner(fr)) { StringBuilder sb = new StringBuilder(); while (scanner.hasNext()) { sb.append(scanner.nextLine()).append("\n"); System.out.println(sb.toString()); } catch (IOException e) { System.err.format("IOException: %s%n", e);
From:一号门
Previous:Java How to send Email
COMMENTS