要读取的文件
name.txt
tom
jack
steve
使用Stream读按行取文件内容
package com.example.demo;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void testStream() throws IOException {
URL url = this.getClass().getResource("/name.txt");
// @since 1.8
try (Stream<String> lines = Files.lines(Paths.get(url.getPath()))) {
lines.forEach(System.out::println);
}
}
}
读取结果
tom
jack
steve
参考
https://www.liaoxuefeng.com/wiki/1252599548343744/1322655160467490