Class FileUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Registerslistener
so that it will receiveFileEvent
s fromFileSystem
s providing instances ofFileObject
convertible tojava.io.File
.static void
addFileChangeListener
(FileChangeListener listener, File path) Adds a listener to changes in a given path.static void
addRecursiveListener
(FileChangeListener listener, File path) Works likeaddRecursiveListener(listener, path, null, null)
.static void
addRecursiveListener
(FileChangeListener listener, File path, FileFilter recurseInto, Callable<Boolean> stop) Adds a listener to changes under given path.static void
addRecursiveListener
(FileChangeListener listener, File path, Callable<Boolean> stop) Works likeaddRecursiveListener(listener, path, null, stop)
.static boolean
affectsOrder
(FileAttributeEvent event) Checks whether a change in a given file attribute would affect the result ofgetOrder(java.util.Collection<org.openide.filesystems.FileObject>, boolean)
.static File
archiveOrDirForURL
(URL entry) Convert a classpath-type URL to a corresponding file.static void
copy
(InputStream is, OutputStream os) Copies stream of files.static void
copyAttributes
(FileObject source, FileObject dest) Copies attributes from one file to another.static void
copyAttributes
(FileObject source, FileObject dest, java.util.function.BiFunction<String, Object, Object> attrTransformer) Copies attributes from one file to another.static FileObject
copyFile
(FileObject source, FileObject destFolder, String newName) Copies file to the selected folder.static FileObject
copyFile
(FileObject source, FileObject destFolder, String newName, String newExt) Copies file to the selected folder.static FileObject
createData
(File data) Returns FileObject for a data file.static FileObject
createData
(FileObject folder, String name) Returns a data file on given filesystem if such a data file exists.static FileObject
createFolder
(File folder) Returns FileObject for a folder.static FileObject
createFolder
(FileObject folder, String name) Returns a folder on given filesystem if such a folder exists.static FileSystem
Factory method that creates an empty implementation of a filesystem that completely resides in a memory.Default attribute transformer forcopyAttributes(org.openide.filesystems.FileObject, org.openide.filesystems.FileObject, java.util.function.BiFunction)
that skips common transient attributes defined or used by the Platform.static void
extractJar
(FileObject fo, InputStream is) Deprecated.Use of XML filesystem layers generally obsoletes this method.static FileObject
findBrother
(FileObject fo, String ext) Finds brother file with same base name but different extension.static String
findFreeFileName
(FileObject folder, String name, String ext) Finds an unused file name similar to that requested in the same folder.static String
findFreeFolderName
(FileObject folder, String name) Finds an unused folder name similar to that requested in the same parent folder.static FileObject[]
Deprecated.UsetoFileObject(java.io.File)
instead.static URL
getArchiveFile
(URL url) Returns the URL of the archive file containing the file referred to by an archive (eg.static FileObject
Returns a FileObject representing an archive file containing the FileObject given by the parameter.static URL
getArchiveRoot
(URL url) Returns a URL representing the root of an archive.static FileObject
Returns a FileObject representing the root folder of an archive.static FileObject
getConfigFile
(String path) ReturnsFileObject
from the NetBeans default (system, configuration) filesystem ornull
if does not exist.static <T> T
getConfigObject
(String path, Class<T> type) Finds a config object under given path.static FileObject
Returns the root of the NetBeans default (system, configuration) filesystem.static String
getExtension
(String fileName) Gets the extension of a specified file name.static String
Get an appropriate display name for a file object.static String
getMIMEType
(String ext) Deprecated.usegetMIMEType(FileObject)
orgetMIMEType(FileObject, String[])
as MIME cannot be generally detected by file object extension.static String
Resolves MIME type.static String
getMIMEType
(FileObject fo, String... withinMIMETypes) Resolves MIME type.getMIMETypeExtensions
(String mimeType) Returns list of file extensions associated with specified MIME type.static List
<FileObject> getOrder
(Collection<FileObject> children, boolean logWarnings) Sorts some sibling file objects.static String
getRelativePath
(FileObject folder, FileObject fo) Gets a relative resource path between folder and fo.static FileObject
getSystemConfigFile
(String path) ReturnsFileObject
from the default filesystem, ornull
if the file does not exist.static <T> T
getSystemConfigObject
(String path, Class<T> type) Finds a config object under the given path, in system-wide configuration.static FileObject
Returns the root of the NetBeans default (system, configuration) filesystem.static boolean
isArchiveArtifact
(URL url) Tests if anURL
denotes a file inside an archive.static boolean
Tests if an file is inside an archive.static boolean
isArchiveFile
(URL url) Tests if a URL represents a java archive.static boolean
Tests if a file represents a java archive.static boolean
isParentOf
(FileObject folder, FileObject fo) Recursively checks whether the file is underneath the folder.static boolean
Check whether some FileObject is a recursive symbolic link.static boolean
isValidFileName
(String fileName) Determines whether the string forms a valid filename (without a path component).static FileObject
moveFile
(FileObject source, FileObject destFolder, String newName) Moves file to the selected folder.static URLStreamHandler
Deprecated.No longer used.static File
normalizeFile
(File file) Normalize a file path to a clean form.static String
normalizePath
(String path) SeenormalizeFile(java.io.File)
for detailsstatic void
preventFileChooserSymlinkTraversal
(JFileChooser chooser, File currentDirectory) Deprecated.static void
Refreshes allFileObject
that represent filesFile.listRoots()
and their children recursively.static void
refreshFor
(File... files) Refreshes all necessary filesystems.static void
Unregisterslistener
so that it will no longer receiveFileEvent
s fromFileSystem
s providing instances ofFileObject
convertible tojava.io.File
static void
removeFileChangeListener
(FileChangeListener listener, File path) Removes a listener to changes in a given path.static void
removeRecursiveListener
(FileChangeListener listener, File path) Removes a listener to changes under given path.static final void
runAtomicAction
(Runnable atomicCode) Executes atomic action.static final void
runAtomicAction
(FileSystem.AtomicAction atomicCode) Executes atomic action.static void
setMIMEType
(String extension, String mimeType) Registers specified extension to be recognized as specified MIME type.static void
setOrder
(List<FileObject> children) Imposes an order on some sibling file objects.static File
toFile
(FileObject fo) Finds appropriate java.io.File to FileObject if possible.static FileObject
toFileObject
(File file) Converts a disk file to a matching file object.static FileObject
toFileObject
(Path path) Converts a Path to a FileObject if that is possible.static Path
toPath
(FileObject fo) Finds appropriate java.nio.file.Path to FileObject if possible.static URL
urlForArchiveOrDir
(File entry) Convert a file such as would be shown in a classpath entry into a proper folder URL.static FileChangeListener
weakFileChangeListener
(FileChangeListener l, Object source) Creates a weak implementation of FileChangeListener.static FileStatusListener
weakFileStatusListener
(FileStatusListener l, Object source) Creates a weak implementation of FileStatusListener.
-
Method Details
-
refreshFor
Refreshes all necessary filesystems. Not all instances ofFileObject
are refreshed but just those that represent passedfiles
and their children recursively.- Parameters:
files
-- Since:
- 7.6
-
refreshAll
public static void refreshAll()Refreshes allFileObject
that represent filesFile.listRoots()
and their children recursively.- Since:
- 7.7
-
addFileChangeListener
Registerslistener
so that it will receiveFileEvent
s fromFileSystem
s providing instances ofFileObject
convertible tojava.io.File
.- Parameters:
fcl
-- Since:
- 7.7
- See Also:
-
removeFileChangeListener
Unregisterslistener
so that it will no longer receiveFileEvent
s fromFileSystem
s providing instances ofFileObject
convertible tojava.io.File
- Parameters:
fcl
-- Since:
- 7.7
- See Also:
-
addFileChangeListener
Adds a listener to changes in a given path. It permits you to listen to a file which does not yet exist, or continue listening to it after it is deleted and recreated, etc.
When given path represents a file (path.isDirectory() == false
)- fileDataCreated event is fired when the file is created
- fileDeleted event is fired when the file is deleted
- fileChanged event is fired when the file is modified
- fileRenamed event is fired when the file is renamed
- fileAttributeChanged is fired when FileObject's attribute is changed
path.isDirectory() == true
)- fileFolderCreated event is fired when the folder is created or a child folder created
- fileDataCreated event is fired when a child file is created
- fileDeleted event is fired when the folder is deleted or a child file/folder removed
- fileChanged event is fired when a child file is modified
- fileRenamed event is fired when the folder is renamed or a child file/folder is renamed
- fileAttributeChanged is fired when FileObject's attribute is changed
- Parameters:
listener
- FileChangeListener to listen to changes in pathpath
- File path to listen to (even not existing)- Since:
- org.openide.filesystems 7.20
- See Also:
-
removeFileChangeListener
Removes a listener to changes in a given path.- Parameters:
listener
- FileChangeListener to be removedpath
- File path in which listener was listening- Throws:
IllegalArgumentException
- if listener was not listening to given path- Since:
- org.openide.filesystems 7.20
- See Also:
-
addRecursiveListener
Works likeaddRecursiveListener(listener, path, null, null)
.- Parameters:
listener
- FileChangeListener to listen to changes in pathpath
- File path to listen to (even not existing)- Since:
- org.openide.filesystems 7.28
-
addRecursiveListener
public static void addRecursiveListener(FileChangeListener listener, File path, Callable<Boolean> stop) Works likeaddRecursiveListener(listener, path, null, stop)
.- Parameters:
listener
- FileChangeListener to listen to changes in pathpath
- File path to listen to (even not existing)stop
- an interface to interrupt the process of registering the listener. If thecall
returns true, the process of registering the listener is immediately interrupted- Since:
- org.openide.filesystems 7.37
- See Also:
-
addRecursiveListener
public static void addRecursiveListener(FileChangeListener listener, File path, FileFilter recurseInto, Callable<Boolean> stop) Adds a listener to changes under given path. It permits you to listen to a file which does not yet exist, or continue listening to it after it is deleted and recreated, etc.
When given path represents a file (path.isDirectory() == false
), this code behaves exactly likeaddFileChangeListener(org.openide.filesystems.FileChangeListener, java.io.File)
. Usually the path shall represent a folder (path.isDirectory() == true
)- fileFolderCreated event is fired when the folder is created or a child folder created
- fileDataCreated event is fired when a child file is created
- fileDeleted event is fired when the folder is deleted or a child file/folder removed
- fileChanged event is fired when a child file is modified
- fileRenamed event is fired when the folder is renamed or a child file/folder is renamed
- fileAttributeChanged is fired when FileObject's attribute is changed
As registering of the listener can take a long time, especially on deep hierarchies, it is possible provide a callbackThose who providestop
. This stop object is guaranteed to be called once per every folder on the default (when masterfs module is included) implemention. If the call tostop.call()
returns true, then the registration of next recursive items is interrupted. The listener may or may not get some events from already registered folders.recurseInto
callback can prevent the system to enter, and register operating system level listeners to certain subtrees under the providedpath
. This does not prevent delivery of changes, if they are made via the filesystem API. External changes however will not be detected.- Parameters:
listener
- FileChangeListener to listen to changes in pathpath
- File path to listen to (even not existing)recurseInto
- a file filter that may returnfalse
when a folder should not be traversed into and external changes in it ignored.null
recurses into all subfoldersstop
- an interface to interrupt the process of registering the listener. If thecall
returns true, the process of registering the listener is immediately interrupted.null
value disables this kind of callback.- Since:
- 7.61
-
removeRecursiveListener
Removes a listener to changes under given path.- Parameters:
listener
- FileChangeListener to be removedpath
- File path in which listener was listening- Throws:
IllegalArgumentException
- if listener was not listening to given path- Since:
- org.openide.filesystems 7.28
- See Also:
-
runAtomicAction
Executes atomic action. For more info seeFileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
.All events about filesystem changes (related to events on all affected instances of
FileSystem
) are postponed after the wholeatomicCode
is executed.- Parameters:
atomicCode
- code that is supposed to be run as atomic action. SeeFileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
- Throws:
IOException
- Since:
- 7.5
-
runAtomicAction
Executes atomic action. For more info seeFileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
.All events about filesystem changes (related to events on all affected instances of
FileSystem
) are postponed after the wholeatomicCode
is executed.- Parameters:
atomicCode
- code that is supposed to be run as atomic action. SeeFileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
- Since:
- 7.5
-
createFolder
Returns FileObject for a folder. If such a folder does not exist then it is created, including any necessary but nonexistent parent folders. Note that if this operation fails it may have succeeded in creating some of the necessary parent folders.- Parameters:
folder
- folder to be created- Returns:
- FileObject for a folder
- Throws:
IOException
- if the creation fails- Since:
- 7.0
-
createData
Returns FileObject for a data file. If such a data file does not exist then it is created, including any necessary but nonexistent parent folders. Note that if this operation fails it may have succeeded in creating some of the necessary parent folders.- Parameters:
data
- data file to be created- Returns:
- FileObject for a data file
- Throws:
IOException
- if the creation fails- Since:
- 7.0
-
copy
Copies stream of files.Please be aware, that this method doesn't close any of passed streams.
- Parameters:
is
- input streamos
- output stream- Throws:
IOException
-
createMemoryFileSystem
Factory method that creates an empty implementation of a filesystem that completely resides in a memory.To specify the MIME type of a data file without using a MIME resolver, set the
mimeType
file attribute.Since 7.42, a
URLMapper
is available for files (and folders) in memory filesystems. These URLs are valid only so long as the filesystem has not been garbage-collected, so hold the filesystem (or a file in it) strongly for as long as you expect the URLs to be in play.- Returns:
- a blank writable filesystem
- Since:
- 4.43
-
copyFile
public static FileObject copyFile(FileObject source, FileObject destFolder, String newName, String newExt) throws IOException Copies file to the selected folder. This implementation simply copies the file by stream content. Since version 9.32, the file POSIX permissions are copied as well.- Parameters:
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination filenewExt
- extension of destination file- Returns:
- the created file object in the destination folder
- Throws:
IOException
- ifdestFolder
is not a folder or does not exist; the destination file already exists; or another critical error occurs during copying
-
copyFile
public static FileObject copyFile(FileObject source, FileObject destFolder, String newName) throws IOException Copies file to the selected folder. This implementation simply copies the file by stream content. Since version 9.32, the file POSIX permissions are copied as well. Uses the extension of the source file.- Parameters:
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination file- Returns:
- the created file object in the destination folder
- Throws:
IOException
- ifdestFolder
is not a folder or does not exist; the destination file already exists; or another critical error occurs during copying
-
moveFile
public static FileObject moveFile(FileObject source, FileObject destFolder, String newName) throws IOException Moves file to the selected folder. This implementation uses a copy-and-delete mechanism, and automatically uses the necessary lock.- Parameters:
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination file- Returns:
- new file object
- Throws:
IOException
- if either thecopy
ordelete
failed
-
createFolder
Returns a folder on given filesystem if such a folder exists. If not then a folder is created, including any necessary but nonexistent parent folders. Note that if this operation fails it may have succeeded in creating some of the necessary parent folders. The name of the new folder can be specified as a multi-component pathname whose components are separated by File.separatorChar or "/" (forward slash).- Parameters:
folder
- where the new folder will be placed inname
- name of the new folder- Returns:
- the new folder
- Throws:
IOException
- if the creation fails
-
createData
Returns a data file on given filesystem if such a data file exists. If not then a data file is created, including any necessary but nonexistent parent folders. Note that if this operation fails it may have succeeded in creating some of the necessary parent folders. The name of data file can be composed as resource name (e. g. org/netbeans/myfolder/mydata ).- Parameters:
folder
- to begin with creation atname
- name of data file as a resource- Returns:
- the data file for given name
- Throws:
IOException
- if the creation fails
-
toFile
Finds appropriate java.io.File to FileObject if possible. If not possible then null is returned. This is the inverse operation oftoFileObject(java.io.File)
.- Parameters:
fo
- FileObject whose corresponding File will be looked for- Returns:
- java.io.File or null if no corresponding File exists.
- Since:
- 1.29
-
toPath
Finds appropriate java.nio.file.Path to FileObject if possible. If not possible then null is returned. This is the inverse operation oftoFileObject(java.io.File)
.- Parameters:
fo
- FileObject whose corresponding Path will be looked for- Returns:
- java.nio.file.Path or null if no corresponding File exists.
- Since:
- 9.32
-
toFileObject
Converts a disk file to a matching file object. This is the inverse operation oftoFile(org.openide.filesystems.FileObject)
.If you are running with
org.netbeans.modules.masterfs
enabled, this method should never return null for a file which exists on disk. For example, to make this method work in unit tests in an Ant-based module project, right-click Unit Test Libraries, Add Unit Test Dependency, check Show Non-API Modules, select Master Filesystem. (Also right-click the new Master Filesystem node, Edit, uncheck Include in Compile Classpath.) To ensure masterfs (or some other module that can handle the conversion) is present put following line into your module manifest:OpenIDE-Module-Needs: org.openide.filesystems.FileUtil.toFileObject
- Parameters:
file
- a disk file (may or may not exist). This file must be normalized.- Returns:
- a corresponding file object, or null if the file does not exist
or there is no
URLMapper
available to convert it - Since:
- 4.29
-
toFileObject
Converts a Path to a FileObject if that is possible. It uses thetoFileObject(java.io.File)
method withpath.toFile()
. if the conversion is not possible for some reasonnull
is returned.- Parameters:
path
- thePath
to be converted- Returns:
- the
FileObject
representing thepath
ornull
- Since:
- 9.32
-
fromFile
Deprecated.UsetoFileObject(java.io.File)
instead.Finds appropriate FileObjects to java.io.File if possible. If not possible then empty array is returned. More FileObjects may correspond to one java.io.File that`s why array is returned.- Parameters:
file
- File whose corresponding FileObjects will be looked for. The file has to be "normalized" otherwise IllegalArgumentException is thrown. SeenormalizeFile(java.io.File)
for how to do that.- Returns:
- corresponding FileObjects or empty array if no corresponding FileObject exists.
- Since:
- 1.29
-
copyAttributes
Copies attributes from one file to another. Note: several special attributes will not be copied, as they should semantically be transient. These include attributes used by the template wizard (but not the template attribute itself).- Parameters:
source
- source file objectdest
- destination file object- Throws:
IOException
- if the copying failed
-
defaultAttributesTransformer
Default attribute transformer forcopyAttributes(org.openide.filesystems.FileObject, org.openide.filesystems.FileObject, java.util.function.BiFunction)
that skips common transient attributes defined or used by the Platform. Custom Attribute Transformers should delegate to this instance.- Returns:
- default attribute transformer instance.
- Since:
- 9.27
-
copyAttributes
public static void copyAttributes(FileObject source, FileObject dest, java.util.function.BiFunction<String, Object, throws IOExceptionObject> attrTransformer) Copies attributes from one file to another. Note: several special attributes will not be copied, as they should semantically be transient. These include attributes used by the template wizard (but not the template attribute itself). IfattrTransformer
is specified, it is called for each attribute that is about to be copied. The returned value will be written to the target. IfattrTransformer
returnsnull
, the attribute will be skipped. The Transformer should delegate todefaultAttributesTransformer()
to conform to the usual transient attribute conventions - unless it really intends to copy such otherwise transient attributes.- Parameters:
source
- source file objectdest
- destination file objectattrTransformer
- callback to transform or filter attribute values. Can benull
.- Throws:
IOException
- if the copying failed- Since:
- 9.27
-
extractJar
Deprecated.Use of XML filesystem layers generally obsoletes this method. For tests, useorg.openide.util.test.TestFileUtils#unpackZipFile
.Extract jar file into folder represented by file object. If the JAR contains files with name filesystem.attributes, it is assumed that these files has been created by DefaultAttributes implementation and the content of these files is treated as attributes and added to extracted files.META-INF/
directories are skipped over.- Parameters:
fo
- file object of destination folderis
- input stream of jar file- Throws:
IOException
- if the extraction fails
-
getExtension
-
findFreeFileName
Finds an unused file name similar to that requested in the same folder. The specified file name is used if that does not yet exist or isisVirtual
. Otherwise, the first available name of the formbasename_nnn.ext
(counting from one) is used.Caution: this method does not lock the parent folder to prevent race conditions: i.e. it is possible (though unlikely) that the resulting name will have been created by another thread just as you were about to create the file yourself (if you are, in fact, intending to create it just after this call). Since you cannot currently lock a folder against child creation actions, the safe approach is to use a loop in which a free name is retrieved; an attempt is made to
create
that file; and upon anIOException
during creation, retry the loop up to a few times before giving up.- Parameters:
folder
- parent foldername
- preferred base name of fileext
- extension to use (or null)- Returns:
- a free file name (without the extension)
-
findFreeFolderName
Finds an unused folder name similar to that requested in the same parent folder.See caveat for
findFreeFileName
.- Parameters:
folder
- parent foldername
- preferred folder name- Returns:
- a free folder name
- See Also:
-
getRelativePath
Gets a relative resource path between folder and fo.- Parameters:
folder
- root of filesystem or any other folder in folders hierarchyfo
- arbitrary FileObject in folder's tree (including folder itself)- Returns:
- relative path between folder and fo. The returned path never
starts with a '/'. It never ends with a '/'. Specifically, if
folder==fo, returns "". Returns
null
if fo is not in folder's tree. - Since:
- 4.16
- See Also:
-
findBrother
Finds brother file with same base name but different extension.- Parameters:
fo
- the file to find the brother for ornull
ext
- extension for the brother file- Returns:
- a brother file (with the requested extension and the same parent folder as the original) or
null
if the brother file does not exist or the original file wasnull
-
getMIMEType
Deprecated.usegetMIMEType(FileObject)
orgetMIMEType(FileObject, String[])
as MIME cannot be generally detected by file object extension.Obtain MIME type for a well-known extension. If there is a case-sensitive match, that is used, else will fall back to a case-insensitive match.- Parameters:
ext
- the extension:"jar"
,"zip"
, etc.- Returns:
- the MIME type for the extension, or
null
if the extension is unrecognized
-
getMIMEType
Resolves MIME type. Registered resolvers are invoked and used to achieve this goal. Resolvers must subclass MIMEResolver.- Parameters:
fo
- whose MIME type should be recognized- Returns:
- the MIME type for the FileObject, or
null
if the FileObject is unrecognized. It may returncontent/unknown
instead ofnull
.
-
getMIMEType
Resolves MIME type. Registered resolvers are invoked and used to achieve this goal. Resolvers must subclass MIMEResolver. By default it is possible for the method to returncontent/unknown
instead ofnull
- if you want to avoid such behavior, includenull
in the list of requestedwithinMIMETypes
- in such case the return value is guaranteed to be one of the values inwithinMIMETypes
ornull
.Example: Check if some file is Java source file or text file:
FileUtil.getMIMEType(fo, null, "text/x-java", "text/plain") != null
- Parameters:
fo
- whose MIME type should be recognizedwithinMIMETypes
- an array of MIME types. Only resolvers whoseMIMEResolver#getMIMETypes()
contain one or more of the requested MIME types will be asked if they recognize the file. It is possible for the resulting MIME type to not be a member of this list.- Returns:
- the MIME type for the FileObject, or
null
if the FileObject is unrecognized. - Since:
- 7.13
-
setMIMEType
Registers specified extension to be recognized as specified MIME type. If MIME type parameter is null, it cancels previous registration. Note that you may register a case-sensitive extension if that is relevant (for example *.C for C++) but if you register a lowercase extension it will by default apply to uppercase extensions too on Windows.- Parameters:
extension
- the file extension to be registeredmimeType
- the MIME type to be registered for the extension ornull
to deregister- See Also:
-
getMIMETypeExtensions
Returns list of file extensions associated with specified MIME type. In other words files with those extensions are recognized as specified MIME type in NetBeans' filesystem. It never returnsnull
.- Parameters:
mimeType
- the MIME type (e.g. image/gif)- Returns:
- list of file extensions associated with specified MIME type, never
null
- Since:
- org.openide.filesystems 7.18
- See Also:
-
nbfsURLStreamHandler
Deprecated.No longer used. -
isParentOf
Recursively checks whether the file is underneath the folder. It checks whether the file and folder are located on the same filesystem, in such case it checks the parentFileObject
of the file recursively until the folder is found or the root of the filesystem is reached.Warning: this method will return false in the case that
folder == fo
.- Parameters:
folder
- the root of folders hierarchy to search info
- the file to search for- Returns:
true
, iffo
lies somewhere underneath thefolder
,false
otherwise- Since:
- 3.16
-
isRecursiveSymbolicLink
Check whether some FileObject is a recursive symbolic link.- Parameters:
fo
- FileObject to check.- Returns:
- True if the FileObject represents a symbolic link referring to a directory that is parent of this FileObject, false otherwise.
- Throws:
IOException
- If some I/O problem occurs.- Since:
- openide.filesystem/9.4
-
weakFileChangeListener
Creates a weak implementation of FileChangeListener.- Parameters:
l
- the listener to delegate tosource
- the source that the listener should detach from when listenerl
is freed, can benull
- Returns:
- a FileChangeListener delegating to
l
. - Since:
- 4.10
-
weakFileStatusListener
Creates a weak implementation of FileStatusListener.- Parameters:
l
- the listener to delegate tosource
- the source that the listener should detach from when listenerl
is freed, can benull
- Returns:
- a FileChangeListener delegating to
l
. - Since:
- 4.10
-
getFileDisplayName
Get an appropriate display name for a file object. If the file corresponds to a path on disk, this will be the disk path. Otherwise the name will mention the filesystem name or archive name in case the file comes from archive and relative path. Relative path will be mentioned just in case that passedFileObject
isn't rootFileObject.isRoot()
.- Parameters:
fo
- a file object- Returns:
- a display name indicating where the file is
- Since:
- 4.39
-
normalizePath
SeenormalizeFile(java.io.File)
for details- Parameters:
path
- file path to normalize- Returns:
- normalized file path
- Since:
- 7.42
-
normalizeFile
Normalize a file path to a clean form. This method may for example make sure that the returned file uses the natural case on Windows; that old Windows 8.3 filenames are changed to the long form; that relative paths are changed to be absolute; that.
and..
sequences are removed; etc. UnlikeFile.getCanonicalFile()
this method will not traverse symbolic links on Unix.This method involves some overhead and should not be called frivolously. Generally it should be called on incoming pathnames that are gotten from user input (including filechoosers), configuration files, Ant properties, etc. Internal calculations should not need to renormalize paths since
File.listFiles()
,File.getParentFile()
, etc. will not produce abnormal variants.- Parameters:
file
- file to normalize- Returns:
- normalized file
- Since:
- 4.48
-
getArchiveRoot
Returns a FileObject representing the root folder of an archive. Clients may need to first callisArchiveFile(FileObject)
to determine if the file object refers to an archive file.- Parameters:
fo
- a java archive file, by default ZIP and JAR are supported- Returns:
- a virtual archive root folder, or null if the file is not actually an archive
- Since:
- 4.48
-
getArchiveRoot
Returns a URL representing the root of an archive. Clients may need to first callisArchiveFile(URL)
to determine if the URL refers to an archive file.- Parameters:
url
- of a java archive file, by default ZIP and JAR are supported- Returns:
- the archive (eg.
jar
) protocol URL of the root of the archive. - Since:
- 4.48
-
getArchiveFile
Returns a FileObject representing an archive file containing the FileObject given by the parameter. Remember that any path within the archive is discarded so you may need to check for non-root entries.- Parameters:
fo
- a file in an archive filesystem- Returns:
- the file corresponding to the archive itself,
or null if
fo
is not an archive entry - Since:
- 4.48
-
getArchiveFile
Returns the URL of the archive file containing the file referred to by an archive (eg.jar
) protocol URL. Remember that any path within the archive is discarded so you may need to check for non-root entries.- Parameters:
url
- a URL- Returns:
- the embedded archive URL, or null if the URL is not an
archive protocol URL containing
!/
- Since:
- 4.48
-
isArchiveFile
Tests if a file represents a java archive. By default the JAR or ZIP archives are supported.- Parameters:
fo
- the file to be tested- Returns:
- true if the file looks like a java archive
- Since:
- 4.48
-
isArchiveFile
Tests if a URL represents a java archive. By default the JAR or ZIP archives are supported. If there is no such file object, the test is done by heuristic: any URL with an extension is treated as an archive.- Parameters:
url
- a URL to a file- Returns:
- true if the URL seems to represent a java archive
- Since:
- 4.48
-
isArchiveArtifact
Tests if an file is inside an archive.- Parameters:
fo
- the file to be tested- Returns:
- true if the file is inside an archive
- Since:
- 9.10
-
isArchiveArtifact
-
urlForArchiveOrDir
Convert a file such as would be shown in a classpath entry into a proper folder URL. If the file looks to represent a directory, afile
URL will be created. If it looks to represent a ZIP archive, ajar
URL will be created.- Parameters:
entry
- a file or directory name- Returns:
- an appropriate classpath URL which will always end in a slash (
/
), or null for an existing file which does not look like a valid archive - Since:
- org.openide.filesystems 7.8
-
archiveOrDirForURL
Convert a classpath-type URL to a corresponding file. If it is ajar
URL representing the root folder of a local disk archive, that archive file will be returned. If it is afile
URL representing a local disk folder, that folder will be returned.- Parameters:
entry
- a classpath entry or similar URL- Returns:
- a corresponding file, or null for e.g. a network URL or non-root JAR folder entry
- Since:
- org.openide.filesystems 7.8
-
preventFileChooserSymlinkTraversal
@Deprecated public static void preventFileChooserSymlinkTraversal(JFileChooser chooser, File currentDirectory) Deprecated.Just useJFileChooser.setCurrentDirectory(java.io.File)
. JDK 6 does not have this bug.Make sure that a JFileChooser does not traverse symlinks on Unix.- Parameters:
chooser
- a file choosercurrentDirectory
- if not null, a file to set as the current directory usingJFileChooser.setCurrentDirectory(java.io.File)
without canonicalizing- Since:
- org.openide/1 4.42
- See Also:
-
getOrder
public static List<FileObject> getOrder(Collection<FileObject> children, boolean logWarnings) throws IllegalArgumentException Sorts some sibling file objects.Normally this is done by looking for numeric file attributes named
position
on the children; children with a lower position number are placed first. Now-deprecated relative ordering attributes of the formearlier/later
may also be used; if the above attribute has a boolean value oftrue
, then the file namedearlier
will be sorted somewhere (not necessarily directly) before the file namedlater
. Numeric and relative attributes may also be mixed.The sort is stable at least to the extent that if there is no ordering information whatsoever, the returned list will be in the same order as the incoming collection.
- Parameters:
children
- zero or more files (or folders); must all have the sameFileObject.getParent()
logWarnings
- true to log warnings about relative ordering attributes or other semantic problems, false to keep quiet- Returns:
- a sorted list of the same children
- Throws:
IllegalArgumentException
- in case there are duplicates, or nulls, or the files do not have a common parent- Since:
- org.openide.filesystems 7.2
- See Also:
-
setOrder
Imposes an order on some sibling file objects. After this call, if no other changes have intervened,getOrder(java.util.Collection<org.openide.filesystems.FileObject>, boolean)
on these files should return a list in the same order. Beyond the fact that this call may manipulate theposition
attributes of files in the folder, and may delete deprecated relative ordering attributes on the folder, the exact means of setting the order is unspecified.- Parameters:
children
- a list of zero or more files (or folders); must all have the sameFileObject.getParent()
- Throws:
IllegalArgumentException
- in case there are duplicates, or nulls, or the files do not have a common parentIOException
- if new file attributes to order the children cannot be written out- Since:
- org.openide.filesystems 7.2
-
affectsOrder
Checks whether a change in a given file attribute would affect the result ofgetOrder(java.util.Collection<org.openide.filesystems.FileObject>, boolean)
.- Parameters:
event
- an attribute change event- Returns:
- true if the attribute in question might affect the order of some folder
- Since:
- org.openide.filesystems 7.2
-
getConfigFile
ReturnsFileObject
from the NetBeans default (system, configuration) filesystem ornull
if does not exist. If you wish to create the file/folder when it does not already exist, start withgetConfigRoot()
and usecreateData(FileObject, String)
orcreateFolder(FileObject, String)
methods.In environment with multiple contextual Lookups, the method may return different FileObject depending on what Lookup serves the executing thread. If the system-wide (user-independent) configuration is required instead,
getSystemConfigFile(java.lang.String)
should be called instead. If an service instance is created based on the configuration, it is important to decide whether the service instance should live for each context independently (possibly with some private means of communication between instances/users) or all users should share the same instance. In the later case,getSystemConfigFile(java.lang.String)
must be used.- Parameters:
path
- the path from the root of the NetBeans default (system, configuration) filesystem delimited by '/' or empty string to get root folder.- Returns:
- a
FileObject
for given path in the NetBeans default (system, configuration) filesystem ornull
if does not exist - Throws:
NullPointerException
- if the path isnull
- Since:
- org.openide.filesystems 7.19, 9.5 support for multiuser environment
-
getSystemConfigFile
ReturnsFileObject
from the default filesystem, ornull
if the file does not exist. UnlikegetConfigFile(java.lang.String)
, this call returns a FileObject from the system-wide configuration. Because default/config filesystem is used both for configuration and services, Lookup or service providers should use this method in preference togetConfigFile(java.lang.String)
to produce singleton services even in multiple context environment.With the default Lookup implementation, behaviour of
getSystemConfigFile
andgetConfigFile(java.lang.String)
is identical.- Parameters:
path
- the path from the root of the NetBeans default (system, configuration) filesystem delimited by '/' or empty string to get root folder.- Returns:
- a
FileObject
for given path in the NetBeans default (system, configuration) filesystem ornull
if does not exist - Throws:
NullPointerException
- if the path isnull
- Since:
- 9.5
-
getConfigObject
Finds a config object under given path. The path contains the extension of a file e.g.:Actions/Edit/org-openide-actions-CopyAction.instance Services/Browsers/swing-browser.settings
In multi-user setup, this method returns instance specific for the executing user. Important: it returns user-specific instance even though the object is configured in a XML layer, or system-wide configuration; still, the instance will be tied to the user-specific file as served by
getConfigFile(java.lang.String)
.- Parameters:
path
- path to .instance or .settings filetype
- the requested type for given object- Returns:
- either null or instance of requrested type
- Since:
- 7.49, 9.5 support for multi-user environment
-
getSystemConfigObject
Finds a config object under the given path, in system-wide configuration. In the default implementation, this method works just asgetConfigObject(java.lang.String, java.lang.Class<T>)
. In multi-user setups, this method should return an instance shared between potential users; in a sense, it works asgetConfigObject(java.lang.String, java.lang.Class<T>)
prior version 9.5- Parameters:
path
- path to .instance or .settings filetype
- the requested type for given object- Returns:
- either null or instance of requrested type
- Since:
- 9.5
-
getConfigRoot
Returns the root of the NetBeans default (system, configuration) filesystem. It returns configuration root using the current Repository, in the case that multiple Repository instances are created to support multiple execution contexts in the same JVM.- Returns:
- a
FileObject
for the root of the NetBeans default (system, configuration) filesystem - Since:
- org.openide.filesystems 7.19, 9.5 support for multiple local contexts
-
getSystemConfigRoot
Returns the root of the NetBeans default (system, configuration) filesystem. UnlikegetConfigRoot()
, this method always provides the system-wide configuration root.- Returns:
- a
FileObject
for the root of the NetBeans default (system, configuration) filesystem - Since:
- 9.5
-
isValidFileName
Determines whether the string forms a valid filename (without a path component).- Parameters:
fileName
- candidate string- Returns:
- true, if the string can be used to name a file.
- Since:
- 9.24
-