java.io.FileNotFoundException(打开的文件过多) [英] java.io.FileNotFoundException (Too many open files)

查看:3063
本文介绍了java.io.FileNotFoundException(打开的文件过多)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码将一些数据写入文件中:

  BufferedWriter writer = null; 
尝试{
writer = new BufferedWriter(new FileWriter(file));
writer.write(...);
writer.flush();

finally {
if(writer!= null)
writer.close();





$ p多次调用方法后,我得到一个FileNotFoundException,因为打开了太多的文件。

很明显,当我关闭writer流时,java并没有关闭文件句柄。单独关闭FileWriter没有帮助。



有没有。我可以做到强制Java关闭文件?

解决方案

你的代码看起来不错。你可以使用 lsof 来监视文件句柄,你可以在你的应用程序的另一部分泄漏文件句柄。 Linux或Solaris上的 pfiles 。在Windows上,您可以使用 ProcessExplorer


I use the following code to write some data to files:

BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(...);
    writer.flush();
}
finally {
    if (writer != null)
        writer.close();
}

After invoking the method multiple times I got a FileNotFoundException because too many files are open.

Obviously java does not close the file handles when I close the writer stream. Closing the FileWriter separately does not help.

Is there sth. I can do to force java to close the files?

解决方案

Your code looks fine. It could be another part of your application which is leaking file handles.

You can monitor file handles using lsof on Linux or pfiles on Solaris. On Windows, you can use ProcessExplorer.

这篇关于java.io.FileNotFoundException(打开的文件过多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆