Java8流逐行讀取文件
在Java8中,可以使用Files.lines讀取文件的流。
c://lines.txt – 用於測試的簡單文本文件
line1 line2 line3 line4 line5
1. Java 8 讀取文件+流
TestReadFile.java
package com.yiibai.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class TestReadFile { public static void main(String args[]) { String fileName = "c://lines.txt"; //read file into stream, try-with-resources try (Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } }
輸出結果
line1 line2 line3 line4 line5
2. Java 8 讀取文件+流+額外
這個例子說明了如何使用數據流過濾內容,其全部內容轉換為大寫並返回一個列表。
TestReadFile2.java
package com.yiibai.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class TestReadFile2 { public static void main(String args[]) { String fileName = "c://lines.txt"; List<String> list = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(fileName))) { //1. filter line 3 //2. convert all content to upper case //3. convert it into a List list = stream .filter(line -> !line.startsWith("line3")) .map(String::toUpperCase) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } }
輸出結果
LINE1 LINE2 LINE4 LINE5
3. BufferedReader + Stream
一種新的方法lines() ,已經在JDK1.8已經添加,它讓 BufferedReader 返回流的內容。
TestReadFile3.java
package com.yiibai.java8; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class TestReadFile3{ public static void main(String args[]) { String fileName = "c://lines.txt"; List<String> list = new ArrayList<>(); try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) { //br returns as stream and convert it into a List list = br.lines().collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } }
輸出結果
line1 line2 line3 line4 line5
4. BufferedReader和Scanner
上麵我們已經足夠了解Java8和流,現在重溫經典BufferedReader(JDK1.1)和Scanner (JDK1.5)的例子來逐行讀取文件中的行。
4.1 BufferedReader + 嘗試使用資源的例子。
TestReadFile4.java
package com.yiibai.core; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TestReadFile4{ public static void main(String args[]) { String fileName = "c://lines.txt"; try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
4.2 Scanner+使用資源的例子。
TestReadFile5.java
package com.yiibai.core; import java.io.File; import java.io.IOException; import java.util.Scanner; public class TestReadFile5 { public static void main(String args[]) { String fileName = "c://lines.txt"; try (Scanner scanner = new Scanner(new File(fileName))) { while (scanner.hasNext()){ System.out.println(scanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } } }
參考