java.io.IOException: 从文件夹和子文件夹中读取文件时打开的文件太多
java.io.IOException: Too many open files when reading files from folder and subfolders
我正在尝试从文件夹及其子文件夹中读取文件,但在读取某些文件后,它抛出
java.io.IOException: Too many open files
我有超过 80k 个文件需要阅读。我无法关闭文件。也许我有一个错误的方法。
请引导我走上正确的道路。谢谢
这是我的 Logcat
Caused by: java.io.IOException: Too many open files
at java.base/sun.nio.ch.IOUtil.makePipe(Native Method)
at java.base/sun.nio.ch.EPollSelectorImpl.<init>(EPollSelectorImpl.java:83)
at java.base/sun.nio.ch.EPollSelectorProvider.openSelector(EPollSelectorProvider.java:36)
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:167)
代码
try (Stream<Path> filePathStream=Files.walk(Paths.get(FOLDER_PATH))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath.toFile().getAbsoluteFile().toString());
try {
String content = Files.readString(filePath);
System.out.println(filePath.getFileName());
JSONObject jsonObject1 = new JSONObject(content);
// System.out.println(file.getName());
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
request.add(new Config().client().prepareIndex("recipe_json", "default").setSource(yourHashMap1));
} catch (IOException ignore) {
}
这个问题可能有几个原因:
您可能只是同时打开了太多文件。我看不出如何,因为 Files.readString(filePath)
不应该泄漏 。但是,我注意到您正在压缩 IOException
,这可能会丢弃一些重要证据。
您可能正在其他地方打开文件...或管道或套接字。也许在这里:
request.add(new Config().client()
.prepareIndex("recipe_json", "default")
.setSource(yourHashMap1));
I am using Elasticsearch Bulk API and adding the content of the file to bulk at this line of code. does that break the rule?
可能是的。查看 API 文档。我希望您需要 "close" 或 "disconnnect" 客户端。如果你不这样做,那很容易导致套接字泄漏。
我正在尝试从文件夹及其子文件夹中读取文件,但在读取某些文件后,它抛出
java.io.IOException: Too many open files
我有超过 80k 个文件需要阅读。我无法关闭文件。也许我有一个错误的方法。 请引导我走上正确的道路。谢谢 这是我的 Logcat
Caused by: java.io.IOException: Too many open files
at java.base/sun.nio.ch.IOUtil.makePipe(Native Method)
at java.base/sun.nio.ch.EPollSelectorImpl.<init>(EPollSelectorImpl.java:83)
at java.base/sun.nio.ch.EPollSelectorProvider.openSelector(EPollSelectorProvider.java:36)
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:167)
代码
try (Stream<Path> filePathStream=Files.walk(Paths.get(FOLDER_PATH))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath.toFile().getAbsoluteFile().toString());
try {
String content = Files.readString(filePath);
System.out.println(filePath.getFileName());
JSONObject jsonObject1 = new JSONObject(content);
// System.out.println(file.getName());
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
request.add(new Config().client().prepareIndex("recipe_json", "default").setSource(yourHashMap1));
} catch (IOException ignore) {
}
这个问题可能有几个原因:
您可能只是同时打开了太多文件。我看不出如何,因为
Files.readString(filePath)
不应该泄漏 。但是,我注意到您正在压缩IOException
,这可能会丢弃一些重要证据。您可能正在其他地方打开文件...或管道或套接字。也许在这里:
request.add(new Config().client() .prepareIndex("recipe_json", "default") .setSource(yourHashMap1));
I am using Elasticsearch Bulk API and adding the content of the file to bulk at this line of code. does that break the rule?
可能是的。查看 API 文档。我希望您需要 "close" 或 "disconnnect" 客户端。如果你不这样做,那很容易导致套接字泄漏。