zoukankan      html  css  js  c++  java
  • Stream系列(十五)File方法使用

    文件读写

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

  • 相关阅读:
    联表查询用on和where的区别
    了解触发器
    QUIC协议,了解
    SQL Mode
    redis持久化
    Kali Linux渗透测试实战 1.2 环境安装及初始化
    电容降压
    单火取电
    大整数的因子
    最大公约数
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12010282.html
Copyright © 2011-2022 走看看