Java 8:处理文件的供应商抛出“流已经被操作或关闭”。 [英] Java 8: Supplier working with files throws "stream has already been operated upon or closed"

查看:122
本文介绍了Java 8:处理文件的供应商抛出“流已经被操作或关闭”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JUnit Stream 我有以下错误消息:

Working with JUnit and Stream I have the following error message:

java.lang.IllegalStateException: stream has already been operated upon or closed

我做了一项研究,很明显可能重复使用流

I did a research, and is clear is not possible reuse a stream

但是根据这篇文章:

  • Java – Stream has already been operated upon or closed

使用供应商可以解决这个问题。

working with Supplier is possible work around with this problem.

所以我当前代码如下:

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

        Supplier<Stream<String>> supplier = () -> stream;

        logger.info("A");
        logger.info("ABC {}", supplier.get().findFirst().get());
        logger.info("B");
        logger.info("XYZ {}", supplier.get().skip(1050).findFirst().get());
        logger.info("C");

        assertThat(supplier.get().count(), is(1051));

    }
    catch (IOException e) {
        logger.error("{}", e.getMessage());
    }

如何看待我使用 supplier.get ()使用 Stream 根据教程),但 @Test 打印直到 B ,因此 @Test supplier.get()中失败.skip(1050).findFirst()。get()并且它仍然生成相同的错误消息。

How you can see I use the supplier.get() to work with the Stream (it according with the tutorial), but the @Test prints until B, therefore the @Test fails in supplier.get().skip(1050).findFirst().get() and it is still generating the same error message.

我的代码和教程之间的独特区别,mime通过文件工作,教程可以在数组中运行。

The unique difference between my code and the tutorial, the mime works through a File and the tutorial works around an array.

编辑工作没有任何问题?

Something special to edit to work without any problem?

Alpha

我做了以下版本(根据Eugene的代码段)

I did the following edition (according with the snippet code of Eugene)

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

        Supplier<Stream<String>> supplier = () -> stream.collect(Collectors.toList()).stream();

        logger.info("A");
        logger.info("ABC {}", supplier.get().findFirst().get());
        logger.info("B");
        logger.info("XYZ {}", supplier.get().skip(1050).findFirst().get());
        logger.info("C");

        assertThat(supplier.get().count(), is(1051));

    }

相同的错误信息。

推荐答案

供应商并不神奇,您仍然需要提供新流来自该供应商的所有时间。

Supplier is no magic, you still need to provide a new Stream all the time from that Supplier.

所以你可以这样做:

Supplier<Stream<String>> supplier = () -> Files.lines(Paths.get(fileName));

但这意味着要一直读取文件。您可以将所有行读入一个 List ,将其存储在内存中,并将 stream 存储在其中。

But that would mean to read the file, all the time. You could read all the lines into a single List, store that in memory and stream out of that.

List<String> allLines = Files.readAllLines(Paths.get(fileName));

Supplier<Stream<String>> supplier = () -> allLines.stream();

请注意,即使您链接的教程也会返回一个新的Stream,通过 Stream.of 如下:

Notice that even the tutorial that you have linked returns a new Stream, created via Stream.of like so:

 Supplier<Stream<String>> streamSupplier = () -> Stream.of(array);

这篇关于Java 8:处理文件的供应商抛出“流已经被操作或关闭”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆