【发布时间】:2019-06-19 16:37:20
【问题描述】:
我正在从文件夹中读取多个文件(1000 个大小约为 5mb 的文件)。下面的代码可以很好地读取、加载和存储文件的内容。
public void readAllFiles(String path) {
try (Stream<Path> paths = Files.walk(Paths.get(path)).collect(toList()).parallelStream()) {
paths.forEach(filePath -> {
if (filePath.toFile().exists()) {
String fileName = filePath.getFileName().toString();
try {
List<String> loadedFile = readContent(filePath);
storeFiles(fileName, filePath, loadedFile);
} catch (Exception e) {
LOGGER.info("ERROR WHILE READING THE CONTENT OF FILE");
LOGGER.error(e.getMessage());
}
}
});
} catch (IOException e) {
LOGGER.info("ERROR WHILE READING THE FILES IN PARALLEL");
LOGGER.error(e.getMessage());
}
}
我的问题是在读取文件时我想排除一些文件,例如排除文件读取,例如条件满足(文件名包含“ABC”&&标志为真)
提前感谢您的任何建议。
【问题讨论】:
-
试试这个
Files.walk(Paths.get(path)).parallel() .filter(filePath->someConditions))...
标签: java multithreading java-8 parallel-processing java-stream