文件读写

视频讲解: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分钟视频学习

最新文章

  1. 4.Java网络编程之TCP/UDP
  2. “static”引发的一个错误
  3. The hierarchy of the type is inconsistent错误问题
  4. redis随笔集-使用
  5. .net core注入时作用域的说明
  6. SlideAndDragListView,一个可排序可滑动item的ListView
  7. springmvc web-info目录下无法引入的js文件无效
  8. java学习第6天
  9. iOS开发经验总结(转)
  10. Net常用资源小集
  11. servlet简介
  12. 知方可补不足~用xsl来修饰xml
  13. cxf的使用及安全校验-02创建简单的客户端接口
  14. T-SQL基础(7) - 透视,逆透视和分组集
  15. http报错之return error code:401 unauthorized
  16. JMeter+ant+jenkins自动化持续集成
  17. 【转】python3 urllib.request 网络请求操作
  18. Jersey VS Django-Rest
  19. SSL 3.0曝出Poodle漏洞的解决方案
  20. Linux是cat、tail、head查看文件任意几行的数据

热门文章

  1. OBDSTAR X300 PRO3详细评论
  2. linux manual free memory
  3. jQuery系列(十三):实现轮播
  4. Django基础之Session版登录验证
  5. AcWing:177. 噩梦(bfs)
  6. redis多实例与主从同步及高级特性(数据过期机制,持久化存储)
  7. redis 的使用 及 配置文件解读
  8. &lt;application&gt;节点属性
  9. Ubuntu下使用AMD APP编写OpenCL程序
  10. golang 中国代理