loc*_*chi 6 java file java-8 java-stream
Java 8有一种从文件行创建Stream的方法.在这种情况下,foreach将逐步执行.我有一个格式如下的文本文件..
bunch of lines with text
$$$$
bunch of lines with text
$$$$
Run Code Online (Sandbox Code Playgroud)
我需要将之前的每一行都$$$$
放到Stream中的单个元素中.
换句话说,我需要一串字符串.每个字符串都包含之前的内容$$$$
.
这样做的最佳方式是什么(最小的开销)?
小智 0
你可以尝试
List<String> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
list = stream
.filter(line -> !line.equals("$$$$"))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)