Java在线运行

版本:
运行结果
教程手册
代码仓库
极速运行
交互输入
极速运行模式,不支持键盘输入语句但是拥有更高的运行速度,输出简洁明了 。 点击编辑器上方的运行按钮即刻体验吧。
以下是用户最新保存的代码
3线程输出26个英文 发布于:2021-06-21 18:04 三个线程轮流输出abc 发布于:2021-06-21 17:50 老鼠、两瓶毒酒 发布于:2021-06-20 19:00 Switch case 发布于:2021-06-18 01:27 bitmap图片 发布于:2021-06-13 17:39 研究生老师工资 发布于:2021-06-09 00:45 实验报告Money 发布于:2021-06-08 23:51 水井实验报告 发布于:2021-06-08 23:41 java子类继承 发布于:2021-06-08 22:49 用Console类实现简单的控制台IO 发布于:2021-06-20 15:59 if条件循环 发布于:2021-06-17 22:12 堆排序扩展题目 发布于:2021-06-05 02:15 改进的堆排序 发布于:2021-06-04 23:42 堆排序-Java 发布于:2021-06-04 21:02 越南语声调处理工具 发布于:2021-06-04 15:58 java测试 发布于:2021-06-09 09:58 快速排序-随机取一个数作为划分标准 发布于:2021-06-04 15:01 小和问题-归并排序扩展 发布于:2021-06-04 01:32 归并排序-Java 发布于:2021-06-03 17:30 冒泡排序-Java 发布于:2021-05-28 17:45 选择排序-Java 发布于:2021-05-28 17:43 一键复用最后代码 发布于:2021-05-26 17:02 Java5-13练习题 发布于:2021-05-25 15:06 键盘输入的 发布于:2021-05-28 19:46 Java5-23练习题 发布于:2021-05-25 14:52 运算注意。 发布于:2021-05-23 18:04 转义字符的使用 发布于:2021-05-23 17:45 1·byte short int 在计算是会自动转化为int 2.float double 为近似值,byte short int 转化时可能会精确丢失 3.把大类型转化小的类型时可能会丢失 发布于:2021-05-23 17:24 IReadBook.java 发布于:2021-05-24 16:04 Java5-20练习题 发布于:2021-05-24 13:09 feistel密码 发布于:2021-05-15 10:52 获取当前时间戳 发布于:2021-05-10 14:10 20210510 测试内部类3 发布于:2021-05-10 08:51 20210510 测试内部类2 发布于:2021-05-10 10:38 20210510 测试内部类1 发布于:2021-05-10 08:27 Calendar的生成 发布于:2021-05-09 23:43 计算出生天数 发布于:2021-05-09 23:07 插入排序算法的改进 发布于:2021-05-07 20:56 插入排序算法 发布于:2021-05-07 20:17 怦然心动Java版, linux下效果最好 发布于:2021-05-10 22:41 选择排序算法改进 发布于:2021-05-06 15:44 java截取字符传中符合标准的日期 发布于:2021-05-06 15:37 冒泡排序算法 发布于:2021-05-06 15:44 选择排序算法--2021.05.05 发布于:2021-05-05 16:46 用户名 密码 发布于:2021-04-21 23:12 Book JavaBean 发布于:2021-04-21 22:47 输入输出库 发布于:2021-04-20 22:41 十进制转十六进制代码 发布于:2021-04-10 13:38 按时区获取近多少天的所有日期 发布于:2021-04-19 20:03 构造方法重载 发布于:2021-03-31 16:16 [更多]
显示目录

Java 8 Stream – Read a file line by line



In Java 8, you can use Files.lines to read file as Stream.

c://lines.txt – A simple text file for testing

line1
line2
line3
line4
line5

1. Java 8 Read File + Stream

TestReadFile.java

package com.mkyong.java8;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class TestReadFile {

    public static void main(String args[]) {

        String fileName = "c://lines.txt";

        //read file into stream, try-with-resources
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            stream.forEach(System.out::println);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output

line1
line2
line3
line4
line5

2. Java 8 Read File + Stream + Extra

This example shows you how to use Stream to filter content, convert the entire content to upper case and return it as a List.

TestReadFile2.java

package com.mkyong.java8;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestReadFile2 {

    public static void main(String args[]) {

        String fileName = "c://lines.txt";
        List<String> list = new ArrayList<>();

        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            //1. filter line 3
            //2. convert all content to upper case
            //3. convert it into a List
            list = stream
                    .filter(line -> !line.startsWith("line3"))
                    .map(String::toUpperCase)
                    .collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        list.forEach(System.out::println);

    }

}

Output

LINE1
LINE2
LINE4
LINE5

3. BufferedReader + Stream

A new method lines() has been added since 1.8, it lets BufferedReader returns content as Stream.

TestReadFile3.java

package com.mkyong.java8;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TestReadFile3{

    public static void main(String args[]) {

        String fileName = "c://lines.txt";
        List<String> list = new ArrayList<>();

        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {

            //br returns as stream and convert it into a List
            list = br.lines().collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        list.forEach(System.out::println);

    }

}

Output

line1
line2
line3
line4
line5

4. Classic BufferedReader And Scanner

Enough of Java 8 and Stream, let revisit the classic BufferedReader (JDK1.1) and Scanner (JDK1.5) examples to read a file line by line, it is working still, just developers are moving toward Stream.

4.1 BufferedReader + try-with-resources example.

TestReadFile4.java

package com.mkyong.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestReadFile4{

    public static void main(String args[]) {

        String fileName = "c://lines.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

4.2 Scanner + try-with-resources example.

TestReadFile5.java

package com.mkyong.core;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class TestReadFile5 {

    public static void main(String args[]) {

        String fileName = "c://lines.txt";

        try (Scanner scanner = new Scanner(new File(fileName))) {

            while (scanner.hasNext()){
                System.out.println(scanner.nextLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

References

  1. Java 8 File.lines()
  2. Java 8 Stream
  3. Java BufferedReader
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。

title

使用此草稿 删除草稿

皮肤:

运行模式:

嵌入代码 iframe嵌入: