您当前的位置: 首页 >  Java

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java:使用Stream读按行取文件内容

彭世瑜 发布时间:2020-07-20 11:42:08 ,浏览量:1

要读取的文件

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 lines = Files.lines(Paths.get(url.getPath()))) {
            lines.forEach(System.out::println);
        }

    }
}

读取结果

tom
jack
steve

参考 https://www.liaoxuefeng.com/wiki/1252599548343744/1322655160467490

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1886s