猿问

使用流将文件夹中的图像添加到数组列表

我正在尝试从文件夹路径作为参数传入的文件夹中读取图像,我正在尝试使用流 API 将它们添加到数组列表中,但我不知道我应该在 forEach 中使用什么变量() 添加到我的数组列表


    public void downsampling(String inputPath, String outputPath) {

    ArrayList<BufferedImage> inputSequence = new ArrayList<BufferedImage>();


    try (Stream<Path> paths = Files.walk(Paths.get(inputPath))) {

        paths

                .forEach(inputSequence.add(XXX));

    } catch (IOException e) {

        e.printStackTrace();

    }

}

XXX 应该换成什么?非常感谢


当年话下
浏览 119回答 1
1回答

有只小跳蛙

代码不完整,因为我还应该进行下采样,我刚刚完成输入,当我评论“有效”时,我的意思是@Eran 的评论帮助我正确地引用了我正在流式传输的文件,这是输入代码到目前为止有效,不需要已经对我投反对票了......&nbsp; &nbsp; public void downsampling(String inputPath, String outputPath) {&nbsp; &nbsp; ArrayList<BufferedImage> inputSequence = new ArrayList<BufferedImage>();&nbsp; &nbsp; try (Stream<Path> paths = Files.walk(Paths.get(inputPath))) {&nbsp; &nbsp; &nbsp; &nbsp; paths&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputSequence.add(ImageIO.read((InputStream) x));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答