javanetbeansopenide

Should an OpenIDE FileObject be closed after creation?


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.


Solution

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