Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 23

Download

How do I get at the file that a particular node represents?

In general, it shall be enough to request a FileObject via Lookup:

Node n = ...;
FileObject fo = n.getLookup().lookup(FileObject.class);
if (fo != null) {
    File f = FileUtil.toFile (fo);
    if (f != null) { //if it is null, it is a virtual file -
                     //its filesystem does not represent disk-based storage
       //do something
    }
}

If this does not work for some (strange) reason. You may fallback to old good way and get the DataObject the node represents, and drill down to a file from there

Node n = ...;
DataObject dob = n.getLookup().lookup(DataObject.class);
if (dob == null) {
    // definitely not a file node
} else {
    // could also get all files in the data object, if desired:
    FileObject fo = dob.getPrimaryFile();
    // and if you really need java.io.File
    File f = FileUtil.toFile (fo);
    if (f != null) { //if it is null, it is a virtual file -
                     //its filesystem does not represent disk-based storage
       //do something
    }
}

In the other direction you can use DataObject.find and then DataObject.getNodeDelegate to get a node representing a file object.

Also see DevFaqFileVsFileObject if you need java.io.File for some reason.