Java流-打开的文件太多
我有一个程序,它会时不时地转到几个目录,并对这些目录中的文件进行某种处理 问题是程序有时(每两三天)会达到操作系统打开文件的限制 它是一个运行在RHEL7中的spring引导应用程序 获取文件的方法如下:Java流-打开的文件太多,java,java-stream,Java,Java Stream,我有一个程序,它会时不时地转到几个目录,并对这些目录中的文件进行某种处理 问题是程序有时(每两三天)会达到操作系统打开文件的限制 它是一个运行在RHEL7中的spring引导应用程序 获取文件的方法如下: public File[] getFiles(String dir, int numberOfFiles) throws Exception { final Path baseDir = Paths.get(dir); List<File> filesFromPat
public File[] getFiles(String dir, int numberOfFiles) throws Exception {
final Path baseDir = Paths.get(dir);
List<File> filesFromPath = new ArrayList<File>();
File[] files = null;
final BiPredicate<Path, BasicFileAttributes> predicate = (path, attrs) -> attrs.isRegularFile()
&& String.valueOf(path).endsWith(".xml");
List<Path> result;
try (Stream<Path> fileStream = Files.find(baseDir, 1, predicate).limit(numberOfFiles).onClose(() -> LOG.debug("Closing file stream."))){
result = fileStream.collect(Collectors.toList());
result.forEach(path -> {
path.toString();
File file = path.toFile();
LOG.info("adding {} to process.", file.getName());
filesFromPath.add(file);
});
if (filesFromPath != null && !filesFromPath.isEmpty()) {
files = filesFromPath.toArray(new File[filesFromPath.size()]);
}
} catch (Exception e) {
LOG.error("Error during file opening/closing", e);
}
if (files != null) {
return files;
}
return new File[0];
}
[编辑2]
所有文件的处理方式如下:
private void processFile(File[] files) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
XPath xpathParser = XPathFactory.newInstance().newXPath();
for (int i = 0; i < files.length; i++) {
File file = files[i];
Document doc = db.parse(file);
// DO STUFF
fileUtils.move(file, processedPath);
}
}
private void processFile(文件[]文件)引发异常{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
XPath xpathParser=XPathFactory.newInstance().newXPath();
对于(int i=0;i
谢谢。我遇到了同样的问题。在调用“parse”的情况下,打开了很多文件,我清楚地看到实现打开了一个输入流,但在出现故障时可能无法关闭它。我遇到了同样的问题。在调用“parse”的情况下,打开了很多文件,我清楚地看到实现打开了一个输入流,但在出现故障时可能不会关闭它。在提供的代码中没有打开任何资源。您的问题在其他地方。“是否应该尝试使用资源,关闭流?”只有在流退出时才出现。你可以在try/catch.Jey中打开很多文件,我编辑了原始问题并添加了另一个方法。系统中不再有处理文件的位置。。谢谢你的回答。肯定还有其他地方,因为在你的第二个代码段中没有调用getFiles()。getFiles()在很多地方被调用。但是,由于它返回一个文件[],而该文件没有“close()”,我应该怎么做呢?这个过程是:一旦我们从getFiles()接收到一个文件[],我们就会处理这些文件并调用move()将文件发送到新的目标。所提供的代码中没有打开的资源。您的问题在其他地方。“是否应该尝试使用资源,关闭流?”只有在流退出时才出现。你可以在try/catch.Jey中打开很多文件,我编辑了原始问题并添加了另一个方法。系统中不再有处理文件的位置。。谢谢你的回答。肯定还有其他地方,因为在你的第二个代码段中没有调用getFiles()。getFiles()在很多地方被调用。但是,由于它返回一个文件[],而该文件没有“close()”,我应该怎么做呢?过程是:一旦我们从getFiles()接收到文件[],我们将处理这些文件并调用move()将文件发送到新的目标。这不会提供问题的答案。这不会提供问题的答案。
private void processFile(File[] files) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
XPath xpathParser = XPathFactory.newInstance().newXPath();
for (int i = 0; i < files.length; i++) {
File file = files[i];
Document doc = db.parse(file);
// DO STUFF
fileUtils.move(file, processedPath);
}
}