如何加载文件并使用Java8进行迭代?

如何加载文件并使用Java8进行迭代?,java,java-8,Java,Java 8,我知道这是一个简单的问题。不过,我可以通过谷歌搜索更多的答案,但你的答案可以提供不同的想法。我试图理解Java8中引入的新特性。作为其中的一部分,我编写了一些代码来读取目录中的文件,并将它们放在InputStream列表中。如何使用Java8简化以下代码 File[] gred_files = gred_directory.listFiles(); List<InputStream> gredInputStreamList = new ArrayList<InputStrea

我知道这是一个简单的问题。不过,我可以通过谷歌搜索更多的答案,但你的答案可以提供不同的想法。我试图理解Java8中引入的新特性。作为其中的一部分,我编写了一些代码来读取目录中的文件,并将它们放在InputStream列表中。如何使用Java8简化以下代码

File[] gred_files = gred_directory.listFiles();

List<InputStream> gredInputStreamList = new ArrayList<InputStream>();

for(File gred_file: gred_files) {
if(gred_file.isFile()) {
    InputStream gredStream = new FileInputStream(gred_file);
    if (gredStream != null)
    {
        gredInputStreamList.add(gredStream);
    }
  }
}
File[]gred_files=gred_directory.listFiles();
List gredInputStreamList=new ArrayList();
用于(文件gred_文件:gred_文件){
if(gred_file.isFile()){
InputStream gredStream=新文件InputStream(gred_文件);
if(gredStream!=null)
{
gredInputStreamList.add(gredStream);
}
}
}

我们将不胜感激

你可以这样做

Arrays.stream(gred_files)
      .filter(File::isFile).map(file -> {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            return null;
        })
      .collect(Collectors.toList());
您可以在这里执行
.map(WhateverClassThisIsIn::openStream)

或者更好

Optional<InputStream> openStream(File file) {
    try {
        return Optional.of(new FileInputStream(gred_file));
    } catch (IOException e) {
        return Optional.empty();
    }
}
可选openStream(文件){
试一试{
返回可选的.of(新文件输入流(gred_文件));
}捕获(IOE异常){
返回可选的.empty();
}
}

List gredInputStreamList=Arrays.stream(gred_directory.listFiles())
.filter(文件::isFile)
.map(whateverclassthissisin::openStream)
.filter(可选::isPresent)
.map(可选::get)
.collect(收集器.toList())
避免不必要的
null
值。(虽然,在这样一个紧密的循环中,这并不重要。)

您可以使用

List<InputStream> list = Files.list(gred_directory.toPath())
    .filter(Files::isRegularFile)
    .map(path -> {
        try { return Files.newInputStream(path); }
        catch(IOException ex) { throw new UncheckedIOException(ex); }
    })
    .collect(Collectors.toList());

您可以通过删除
if(gredStream!=null)
检查来简化
new
无法创建
null
,在最坏的情况下,它将引发异常。您不读取文件,只打开文件。这个
列表的目的是什么?@Holger也许以后会用到它们。但你的方向是正确的:打开和使用它们应该是一个紧密的循环。+我喜欢你的助手方法;)由于选中的异常,
.map(FileInputStream::new)
不起作用,但是,如果它起作用,随后的
.filter(o->o!=null)
就过时了,因为
new
操作符永远不能
null
@Holger对于第一个代码示例来说是正确的,所以我删除了它。
.map(file->new FileInputStream(file))
无法工作,因为需要检查
IOException
。@glglgl谢谢:)没有看到检查的异常部分。
Optional<InputStream> openStream(File file) {
    try {
        return Optional.of(new FileInputStream(gred_file));
    } catch (IOException e) {
        return Optional.empty();
    }
}
List<InputStream> gredInputStreamList  = Arrays.stream(gred_directory.listFiles())
    .filter(File::isFile)
    .map(WhateverClassThisIsIn::openStream)
    .filter(Optional::isPresent)
    .map(Optional::get)
    .collect(Collectors.toList())
List<InputStream> list = Files.list(gred_directory.toPath())
    .filter(Files::isRegularFile)
    .map(path -> {
        try { return Files.newInputStream(path); }
        catch(IOException ex) { throw new UncheckedIOException(ex); }
    })
    .collect(Collectors.toList());
List<byte[]> list = Files.list(gred_directory.toPath())
    .filter(Files::isRegularFile)
    .map(path -> {
        try { return Files.readAllBytes(path); }
        catch(IOException ex) { throw new UncheckedIOException(ex); }
    })
    .collect(Collectors.toList());