blob: fd89bfa443f3d58ad55da122803963db70574721 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqFilesFromNodes
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqFilesFromNodes
:description: Apache NetBeans wiki DevFaqFilesFromNodes
:toc: left
:toc-title:
:syntax: true
=== How do I get at the file that a particular node represents?
In general, it shall be enough to request a link:FileObject.asciidoc[FileObject] via link:Lookup.asciidoc[Lookup]:
[source,java]
----
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
[source,java]
----
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 link:DevFaqFileVsFileObject.asciidoc[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 link:http://wiki.netbeans.org/DevFaqFilesFromNodes[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-02-07, and needs to be reviewed.