有没有办法在一个Java8流中读取两个或更多的文件? [英] Is there any way for reading two or more files in one Java8-stream?

查看:371
本文介绍了有没有办法在一个Java8流中读取两个或更多的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢新的Java8 StreamAPI,并且希望不仅仅用于一个文件。
通常,我使用下面的代码:

  Stream< String> lines = Files.lines(Paths.get(/ somepathtofile)); 

但是如果可能的话,如何在一个流中读取两个文件呢?

解决方案

没有任何额外的辅助函数或外部库,最简单的是:

 流<字符串> lines1 = Files.lines(Paths.get(/ somepathtofile)); 
Stream< String> lines2 = Files.lines(Paths.get(/ somepathtoanotherfile));

Stream.concat(lines1,lines)
.filter(...)
.forEach(...);

如果 Files.lines 声明抛出一个检查的异常,你可以做

$ $ $ $ $ $ c $ Stream.of(/ file1,/ file2 )
.map(Paths :: get)
.flatMap(Files :: lines)....

但是,唉,我们不能这样做。有几个解决方法。一个是使你自己的版本 Files.lines 调用标准版本,捕获 IOException ,并重新抛出 UncheckedIOException 。另一种方法是从抛出检查异常的方法中进行功能的更一般的方法。它看起来像这样:

pre $ $ $ $ $ $ $ $ $ $ $ public interface ThrowingFunction< T,R>扩展Function< T,R> {

@Override
public default R apply(T t){
try {
return throwingApply(t);
catch(Exception e){
throw new RuntimeException(e);
}
}

public static< T,R>功能< T,R> (ThrowingFunction< T,R> f){
return f;
}

R throwingApply(T t)throws Exception;

$ / code>

然后是

<$ Stream.of(/ somefile,/ someotherfile,/ yetanotherfile)
.map(Paths :: get)
.flatMap(ThrowingFunction。 wrap(Files :: lines))
.....

在那里经历了为每个功能界面写上类似上面的东西的麻烦。

I like new Java8 StreamAPI and want use it not only for one file. As usually, I use this code:

Stream<String> lines = Files.lines(Paths.get("/somepathtofile"));

But how read two file in one stream if it possibly?

解决方案

Without any extra helper functions or outside libraries, the easiest is:

Stream<String> lines1 = Files.lines(Paths.get("/somepathtofile"));
Stream<String> lines2 = Files.lines(Paths.get("/somepathtoanotherfile"));

Stream.concat(lines1, lines)
    .filter(...)
    .forEach(...);

If Files.lines hadn't been declared to throw a checked exception, you'd be able to do

Stream.of("/file1", "/file2")
     .map(Paths::get)
     .flatMap(Files::lines)....

But, alas, we can't do that. There are several workarounds. One is to make your own version of Files.lines that calls the standard one, catches IOException and rethrows as an UncheckedIOException. Another approach is a more general way to make functions out of methods that throw checked exceptions. It would look something like this:

@FunctionalInterface
public interface ThrowingFunction<T,R> extends Function<T,R> {

    @Override
    public default R apply(T t) {
        try {
            return throwingApply(t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static<T,R> Function<T,R> wrap(ThrowingFunction<T,R> f) {
        return f;
    }

    R throwingApply(T t) throws Exception;
}

and then

Stream.of("/somefile", "/someotherfile", "/yetanotherfile")
        .map(Paths::get)
        .flatMap(ThrowingFunction.wrap(Files::lines))
        .....

There are several libraries out there that went through the trouble of writing something like the above out for every functional interface.

这篇关于有没有办法在一个Java8流中读取两个或更多的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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