OpenIDE FileObject 创建后是否应该关闭?
在 NetBeans 平台上创建模块时,FileObject
对象表示 IDE 虚拟文件系统中的文件。 创建新的 FileObject
很简单,但是 NetBeans 是否完全控制对实际 File
的引用,或者我应该自己关闭 FileObject
吗? 我的代码是这样的:
FileObject appRoot = FileUtil.getConfigRoot().getFileObject("myapp");
try {
FileObject fo = servers.createData(filename);
fo.setAttribute("name", "a name");
fo.setAttribute("desc", "a description");
} catch (IOException ex) {
throws new FileCreationException("Could not create file " + filename, ex);
}
使用上面的代码,我是否保留对实际文件的一些引用,或者我应该获取 FileObject
的 OutputStream
并手动关闭它?
谢谢。
When creating modules on the NetBeans platform, the FileObject
object represents a file in the virtual file system of the IDE. Creating new FileObject
s is simple, but does NetBeans completely control the reference to the actual File
, or should I close FileObject
myself? My code is like this:
FileObject appRoot = FileUtil.getConfigRoot().getFileObject("myapp");
try {
FileObject fo = servers.createData(filename);
fo.setAttribute("name", "a name");
fo.setAttribute("desc", "a description");
} catch (IOException ex) {
throws new FileCreationException("Could not create file " + filename, ex);
}
With the above code, am I leaving open some references to the actual file or should I obtains the OutputStream
of the FileObject
and close it manually?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在深入研究 NetBeans API 和源代码后,我相信我已经找到了自己问题的答案。
上面设置的属性存储在一个特殊的属性文件中。 虚拟文件系统中的每个文件夹都有一个隐藏的属性文件(
.nbattrs
),其中包含为每个FileObject
存储的属性,例如该文件完全由NetBeans控制,无需打开或关闭输入/输出流是必要的。
但是,如果您想要向
FileObject
添加内容而不仅仅是属性,则必须使用InputStream
和OutputStream 的正常 Java 方式来完成此操作
(两者都有 getter 和 setter),并记住相应地关闭流。 例如FileObject
的After digging around in the NetBeans API and source code I believe I've found the answer to my own question.
Attributes as set above are stored in a special attributes file. Each folder in the virtual file system has a hidden attributes file (
.nbattrs
) which contains the attributes stored for eachFileObject
, e.g.This file is completely controlled by NetBeans and no opening or closing of input/output streams are necessary.
If however, you want to add content to the
FileObject
and not mere attributes, you will have to do it the normal Java-way of using theInputStream
andOutputStream
of theFileObject
(both have a getter and setter) and remember to close the streams accordingly. e.g.