Stream系列(十五)File方法使用
2024-09-05 03:56:27
文件读写
视频讲解:https://www.bilibili.com/video/av78612785/
EmployeeTestCase.java
package com.example.demo; import lombok.extern.log4j.Log4j2;
import org.junit.Test; import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors; @Log4j2
public class EmployeeTestCase extends BaseTestCase {
@Test
public void writeRead() throws IOException {
//写入文件
PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(Paths.get("E://text.txt")));
list.stream().forEach(printWriter::println);
printWriter.close();
//读取文件
List<String> content = Files.lines(Paths.get("E://text.txt")).peek(System.out::println).collect(Collectors.toList());
} }
BaseTestCase.java
package com.example.demo; import java.util.Arrays;
import java.util.List; public class BaseTestCase {
protected static final List<Employee> list = Arrays.asList(
new Employee(1, "Alex", 1000),
new Employee(2, "Michael", 2000),
new Employee(3, "Jack", 1500),
new Employee(4, "Owen", 1500),
new Employee(5, "Denny", 2000));
}
结果:
Employee(id=1, name=Alex, salary=1000.0)
Employee(id=2, name=Michael, salary=2000.0)
Employee(id=3, name=Jack, salary=1500.0)
Employee(id=4, name=Owen, salary=1500.0)
Employee(id=5, name=Denny, salary=2000.0)
关注公众号,坚持每天3分钟视频学习
最新文章
- 4.Java网络编程之TCP/UDP
- “static”引发的一个错误
- The hierarchy of the type is inconsistent错误问题
- redis随笔集-使用
- .net core注入时作用域的说明
- SlideAndDragListView,一个可排序可滑动item的ListView
- springmvc web-info目录下无法引入的js文件无效
- java学习第6天
- iOS开发经验总结(转)
- Net常用资源小集
- servlet简介
- 知方可补不足~用xsl来修饰xml
- cxf的使用及安全校验-02创建简单的客户端接口
- T-SQL基础(7) - 透视,逆透视和分组集
- http报错之return error code:401 unauthorized
- JMeter+ant+jenkins自动化持续集成
- 【转】python3 urllib.request 网络请求操作
- Jersey VS Django-Rest
- SSL 3.0曝出Poodle漏洞的解决方案
- Linux是cat、tail、head查看文件任意几行的数据