Java 8 Stream – Read a file line by line
编辑教程In Java 8, you can use Files.lines
to read file as Stream
.
c://lines.txt – A simple text file for testing
line1
line2
line3
line4
line5
1. Java 8 Read File + Stream
TestReadFile.java
package com.mkyong.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();
}
}
}
Output
line1
line2
line3
line4
line5
2. Java 8 Read File + Stream + Extra
This example shows you how to use Stream
to filter content, convert the entire content to upper case and return it as a List
.
TestReadFile2.java
package com.mkyong.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);
}
}
Output
LINE1
LINE2
LINE4
LINE5
3. BufferedReader + Stream
A new method lines()
has been added since 1.8, it lets BufferedReader
returns content as Stream
.
TestReadFile3.java
package com.mkyong.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);
}
}
Output
line1
line2
line3
line4
line5
4. Classic BufferedReader And Scanner
Enough of Java 8 and Stream
, let revisit the classic BufferedReader
(JDK1.1) and Scanner
(JDK1.5) examples to read a file line by line, it is working still, just developers are moving toward Stream
.
4.1 BufferedReader
+ try-with-resources example.
TestReadFile4.java
package com.mkyong.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
+ try-with-resources example.
TestReadFile5.java
package com.mkyong.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();
}
}
}
References
Mos固件,小电视必刷固件
ES6 教程
Vue.js 教程
JSON 教程
jQuery 教程
HTML 教程
HTML 5 教程
CSS 教程
CSS3 教程
JavaScript 教程
DHTML 教程
JSON在线格式化工具
JS在线运行
JSON解析格式化
jsfiddle中国国内版本
JS代码在线运行
PHP代码在线运行
Java代码在线运行
C语言代码在线运行
C++代码在线运行
Python代码在线运行
Go语言代码在线运行
C#代码在线运行
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。
大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
选择支付方式:
立即支付
¥
9.99
无法付款,请点击这里
金额: 0 元
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟