java - 从路径读取文件夹内的文件
问题描述
我需要读取文件夹中的所有文件。这是我的路径 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);
解决方案
此外,您可以使用它来检查子路径 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();
}
});
推荐阅读
- java - java中带有变量类型的圆括号是什么意思?
- javascript - 使用 for 循环向页面动态添加字符串的问题
- git - GIT合并不相关的历史
- vue.js - 为什么通过 scrollBy() 滚动在 Vue 组件更新挂钩中不起作用?
- python - 如何通过标签获取所有元素?
- angular - 显式指定材质下拉选择的类型而不是动态
- geospatial - 可以使用空间索引加速“单个几何内的点”查询吗?
- android - 刷新小部件上的现有数据
- excel - 跨工作表的 Excel VBA 克隆(命名)表
- clojure - clj-http 异常:ExceptionInfo clj-http: 状态 415 clj-http.client/wrap-exceptions/fn--1863 (client.clj:196)