OpenIDE FileObject 创建后是否应该关闭?

发布于 2024-07-23 08:25:55 字数 671 浏览 5 评论 0原文

在 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);            
} 

使用上面的代码,我是否保留对实际文件的一些引用,或者我应该获取 FileObjectOutputStream 并手动关闭它?

谢谢。

When creating modules on the NetBeans platform, the FileObject object represents a file in the virtual file system of the IDE. Creating new FileObjects 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2024-07-30 08:25:55

在深入研究 NetBeans API 和源代码后,我相信我已经找到了自己问题的答案。

上面设置的属性存储在一个特殊的属性文件中。 虚拟文件系统中的每个文件夹都有一个隐藏的属性文件(.nbattrs),其中包含为每个FileObject存储的属性,例如

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE attributes PUBLIC "-//NetBeans//DTD DefaultAttributes 1.0//EN"     
                            "http://www.netbeans.org/dtds/attributes-1_0.dtd">
<attributes version="1.0">
    <fileobject name="dk-i2m-netbeans-smtpdummyservice-mailserver-1244831819713">
        <attr name="name" stringvalue="My test"/>
        <attr name="desc" intvalue="Server for testing outgoing e-mails"/>
    </fileobject>
</attributes>

该文件完全由NetBeans控制,无需打开或关闭输入/输出流是必要的。

但是,如果您想要向 FileObject 添加内容而不仅仅是属性,则必须使用 InputStreamOutputStream 的正常 Java 方式来完成此操作FileObject (两者都有 getter 和 setter),并记住相应地关闭流。 例如

FileObject appRoot = FileUtil.getConfigRoot().getFileObject("myapp");

try {
    FileObject fo = servers.createData(filename);
    fo.setAttribute("name", "a name");
    fo.setAttribute("desc", "a description");

    // Lock the FileObject before writing
    FileLock lock;
    try {
        lock = fo.lock();
    } catch (FileAlreadyLockedException ex) {
        Exceptions.printStackTrace(ex);
        return;
    }

    try {
        OutputStream out = fo.getOutputStream(lock);
        try {
            // Write into the output stream
        } finally {
            // Remember to close the stream
            out.close();
        }
    } finally {
        lock.releaseLock();
    }
} catch (IOException ex) {
    throws new FileCreationException("Could not create file " + filename, ex);            
}

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 each FileObject, e.g.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE attributes PUBLIC "-//NetBeans//DTD DefaultAttributes 1.0//EN"     
                            "http://www.netbeans.org/dtds/attributes-1_0.dtd">
<attributes version="1.0">
    <fileobject name="dk-i2m-netbeans-smtpdummyservice-mailserver-1244831819713">
        <attr name="name" stringvalue="My test"/>
        <attr name="desc" intvalue="Server for testing outgoing e-mails"/>
    </fileobject>
</attributes>

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 the InputStream and OutputStream of the FileObject (both have a getter and setter) and remember to close the streams accordingly. e.g.

FileObject appRoot = FileUtil.getConfigRoot().getFileObject("myapp");

try {
    FileObject fo = servers.createData(filename);
    fo.setAttribute("name", "a name");
    fo.setAttribute("desc", "a description");

    // Lock the FileObject before writing
    FileLock lock;
    try {
        lock = fo.lock();
    } catch (FileAlreadyLockedException ex) {
        Exceptions.printStackTrace(ex);
        return;
    }

    try {
        OutputStream out = fo.getOutputStream(lock);
        try {
            // Write into the output stream
        } finally {
            // Remember to close the stream
            out.close();
        }
    } finally {
        lock.releaseLock();
    }
} catch (IOException ex) {
    throws new FileCreationException("Could not create file " + filename, ex);            
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文