首页 > 解决方案 > 从路径读取文件夹内的文件

问题描述

我需要读取文件夹中的所有文件。这是我的路径 c:/records/today/,路径内部有两个文件 data1.txt 和 data2.txt。获取文件后,我需要阅读并显示它。我已经用第一个文件做了,我只是不知道怎么做。

File file = ResourceUtils.getFile("c:/records/today/data1.txt");        
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);

标签: javaspringeclipsespring-bootmaven

解决方案


此外,您可以使用它来检查子路径 isFile 或 directory

Arrays.stream(ResourceUtils.getFile("c:/records/today/data1.txt").listFiles())
            .filter(File::isFile)
            .forEach(file -> {
                try {
                    String content = new String(Files.readAllBytes(file.toPath()));
                    System.out.println(content);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

推荐阅读