如何找出在Java中打开了文件的对象? [英] How can I find out which object has a file open in Java?

查看:58
本文介绍了如何找出在Java中打开了文件的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出我的Java应用程序中的哪个对象已打开文件.这是用于调试,因此欢迎使用工具或实用程序.

I need to find out which object in my Java app has a file open. This is for debugging, so tools or utilities are welcome.

如果找到哪个对象太具体,那么哪个也将非常有帮助.

If finding which object is too specific, which class would also be very helpful.

推荐答案

那可能很棘手.您可以从使用探查器开始,例如 VisualVM (免费)或

That could be tricky. You could start by using a profiler, like VisualVM (free) or YourKit (not free) to inspect java.io.File objects in memory. If you can find one with the path you're interested in, that might point you in the right direction. The problem is that a File object doesn't necessarily have to exist for an app to still have the file open.

您的下一步可能是在两个中设置断点FileInputStream FileOutputStream (除非您知道文件是正在读取还是写入),请附加调试器,并注意其中的一个文件将打开您的文件.但是,当然,还有其他打开文件的方法.您可能还需要放样 Class.getResourceAsStream()

Your next step might be to set breakpoints in both FileInputStream and FileOutputStream (unless you know whether the file is being read or written to), attach a debugger, and watch for your file to be opened by one of those. But of course, there are other ways of opening files as well. You might also need to stake out things like Class.getResourceAsStream() and URL.openConnection(), for example.

您最后的选择可能是尝试使用无所不知的调试器,例如 ODB TOD .我对他们的经验有限,但是他们声称知道一切".您只需要弄清楚如何提出正确的问题即可.

Your last resort might be to try out an omniscient debugger, like ODB or TOD. I only have limited experience with them, but they claim to "know everything". You'll just have to figure out how to ask the right question.

这篇关于如何找出在Java中打开了文件的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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