Skip to content

DevFaqFilesFromNodes

Antonio Vieiro edited this page Jan 25, 2018 · 1 revision

DevFaqFilesFromNodes

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.

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/DevFaqFilesFromNodes , that was last modified by NetBeans user Jtulach on 2010-07-24T18:53:56Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.

Clone this wiki locally