Class FileUtil

java.lang.Object
org.openide.filesystems.FileUtil

public final class FileUtil extends Object
Common utilities for handling files. This is a dummy class; all methods are static.
  • Method Details

    • refreshFor

      public static void refreshFor(File... files)
      Refreshes all necessary filesystems. Not all instances of FileObject are refreshed but just those that represent passed files and their children recursively.
      Parameters:
      files -
      Since:
      7.6
    • refreshAll

      public static void refreshAll()
      Refreshes all FileObject that represent files File.listRoots() and their children recursively.
      Since:
      7.7
    • addFileChangeListener

      public static void addFileChangeListener(FileChangeListener fcl)
      Registers listener so that it will receive FileEvents from FileSystems providing instances of FileObject convertible to java.io.File.
      Parameters:
      fcl -
      Since:
      7.7
      See Also:
    • removeFileChangeListener

      public static void removeFileChangeListener(FileChangeListener fcl)
      Unregisters listener so that it will no longer receive FileEvents from FileSystems providing instances of FileObject convertible to java.io.File
      Parameters:
      fcl -
      Since:
      7.7
      See Also:
    • addFileChangeListener

      public static void addFileChangeListener(FileChangeListener listener, File path)
      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
      When given path represents 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
      Can only add a given [listener, path] pair once. However a listener can listen to any number of paths. Note that listeners are always held weakly - if the listener is collected, it is quietly removed.
      Parameters:
      listener - FileChangeListener to listen to changes in path
      path - File path to listen to (even not existing)
      Since:
      org.openide.filesystems 7.20
      See Also:
    • removeFileChangeListener

      public static void removeFileChangeListener(FileChangeListener listener, File path)
      Removes a listener to changes in a given path.
      Parameters:
      listener - FileChangeListener to be removed
      path - 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

      public static void addRecursiveListener(FileChangeListener listener, File path)
      Parameters:
      listener - FileChangeListener to listen to changes in path
      path - 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)
      Parameters:
      listener - FileChangeListener to listen to changes in path
      path - File path to listen to (even not existing)
      stop - an interface to interrupt the process of registering the listener. If the call 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 like addFileChangeListener(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
      The above events are delivered for changes in all subdirectories (recursively). It is guaranteed that with each change at least one event is generated. For example adding a folder does not notify about content of the folder, hence one event is delivered. Can only add a given [listener, path] pair once. However a listener can listen to any number of paths. Note that listeners are always held weakly - if the listener is collected, it is quietly removed.
      As registering of the listener can take a long time, especially on deep hierarchies, it is possible provide a callback stop. This stop object is guaranteed to be called once per every folder on the default (when masterfs module is included) implemention. If the call to stop.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.
      Those who provide recurseInto callback can prevent the system to enter, and register operating system level listeners to certain subtrees under the provided path. 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 path
      path - File path to listen to (even not existing)
      recurseInto - a file filter that may return false when a folder should not be traversed into and external changes in it ignored. null recurses into all subfolders
      stop - an interface to interrupt the process of registering the listener. If the call returns true, the process of registering the listener is immediately interrupted. null value disables this kind of callback.
      Since:
      7.61
    • removeRecursiveListener

      public static void removeRecursiveListener(FileChangeListener listener, File path)
      Removes a listener to changes under given path.
      Parameters:
      listener - FileChangeListener to be removed
      path - 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

      public static final void runAtomicAction(FileSystem.AtomicAction atomicCode) throws IOException
      Executes atomic action. For more info see FileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction).

      All events about filesystem changes (related to events on all affected instances of FileSystem) are postponed after the whole atomicCode is executed.

      Parameters:
      atomicCode - code that is supposed to be run as atomic action. See FileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
      Throws:
      IOException
      Since:
      7.5
    • runAtomicAction

      public static final void runAtomicAction(Runnable atomicCode)
      Executes atomic action. For more info see FileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction).

      All events about filesystem changes (related to events on all affected instances of FileSystem) are postponed after the whole atomicCode is executed.

      Parameters:
      atomicCode - code that is supposed to be run as atomic action. See FileSystem.runAtomicAction(org.openide.filesystems.FileSystem.AtomicAction)
      Since:
      7.5
    • createFolder

      public static FileObject createFolder(File folder) throws IOException
      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

      public static FileObject createData(File data) throws IOException
      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

      public static void copy(InputStream is, OutputStream os) throws IOException
      Copies stream of files.

      Please be aware, that this method doesn't close any of passed streams.

      Parameters:
      is - input stream
      os - output stream
      Throws:
      IOException
    • createMemoryFileSystem

      public static FileSystem 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 object
      destFolder - destination folder
      newName - file name (without extension) of destination file
      newExt - extension of destination file
      Returns:
      the created file object in the destination folder
      Throws:
      IOException - if destFolder 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 object
      destFolder - destination folder
      newName - file name (without extension) of destination file
      Returns:
      the created file object in the destination folder
      Throws:
      IOException - if destFolder 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 object
      destFolder - destination folder
      newName - file name (without extension) of destination file
      Returns:
      new file object
      Throws:
      IOException - if either the copy or delete failed
    • createFolder

      public static FileObject createFolder(FileObject folder, String name) throws IOException
      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 in
      name - name of the new folder
      Returns:
      the new folder
      Throws:
      IOException - if the creation fails
    • createData

      public static FileObject createData(FileObject folder, String name) throws IOException
      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 at
      name - name of data file as a resource
      Returns:
      the data file for given name
      Throws:
      IOException - if the creation fails
    • toFile

      public static File toFile(FileObject fo)
      Finds appropriate java.io.File to FileObject if possible. If not possible then null is returned. This is the inverse operation of toFileObject(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

      public static Path toPath(FileObject fo)
      Finds appropriate java.nio.file.Path to FileObject if possible. If not possible then null is returned. This is the inverse operation of toFileObject(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

      public static FileObject toFileObject(File file)
      Converts a disk file to a matching file object. This is the inverse operation of toFile(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

      public static FileObject toFileObject(Path path)
      Converts a Path to a FileObject if that is possible. It uses the toFileObject(java.io.File) method with path.toFile(). if the conversion is not possible for some reason null is returned.
      Parameters:
      path - the Path to be converted
      Returns:
      the FileObject representing the path or null
      Since:
      9.32
    • fromFile

      @Deprecated public static FileObject[] fromFile(File file)
      Deprecated.
      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. See normalizeFile(java.io.File) for how to do that.
      Returns:
      corresponding FileObjects or empty array if no corresponding FileObject exists.
      Since:
      1.29
    • copyAttributes

      public static void copyAttributes(FileObject source, FileObject dest) throws IOException
      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 object
      dest - destination file object
      Throws:
      IOException - if the copying failed
    • defaultAttributesTransformer

      public static java.util.function.BiFunction<String,Object,Object> defaultAttributesTransformer()
      Default attribute transformer for copyAttributes(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,Object> attrTransformer) throws IOException
      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). If attrTransformer is specified, it is called for each attribute that is about to be copied. The returned value will be written to the target. If attrTransformer returns null, the attribute will be skipped. The Transformer should delegate to defaultAttributesTransformer() to conform to the usual transient attribute conventions - unless it really intends to copy such otherwise transient attributes.
      Parameters:
      source - source file object
      dest - destination file object
      attrTransformer - callback to transform or filter attribute values. Can be null.
      Throws:
      IOException - if the copying failed
      Since:
      9.27
    • extractJar

      @Deprecated public static void extractJar(FileObject fo, InputStream is) throws IOException
      Deprecated.
      Use of XML filesystem layers generally obsoletes this method. For tests, use org.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 folder
      is - input stream of jar file
      Throws:
      IOException - if the extraction fails
    • getExtension

      public static String getExtension(String fileName)
      Gets the extension of a specified file name. The extension is everything after the last dot.
      Parameters:
      fileName - name of the file
      Returns:
      extension of the file (or "" if it had none)
    • findFreeFileName

      public static String findFreeFileName(FileObject folder, String name, String ext)
      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 is isVirtual. Otherwise, the first available name of the form basename_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 an IOException during creation, retry the loop up to a few times before giving up.

      Parameters:
      folder - parent folder
      name - preferred base name of file
      ext - extension to use (or null)
      Returns:
      a free file name (without the extension)
    • findFreeFolderName

      public static String findFreeFolderName(FileObject folder, String name)
      Finds an unused folder name similar to that requested in the same parent folder.

      See caveat for findFreeFileName.

      Parameters:
      folder - parent folder
      name - preferred folder name
      Returns:
      a free folder name
      See Also:
    • getRelativePath

      public static String getRelativePath(FileObject folder, FileObject fo)
      Gets a relative resource path between folder and fo.
      Parameters:
      folder - root of filesystem or any other folder in folders hierarchy
      fo - 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

      public static FileObject findBrother(FileObject fo, String ext)
      Finds brother file with same base name but different extension.
      Parameters:
      fo - the file to find the brother for or null
      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 was null
    • getMIMEType

      @Deprecated public static String getMIMEType(String ext)
      Deprecated.
      use getMIMEType(FileObject) or getMIMEType(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

      public static String getMIMEType(FileObject fo)
      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 return content/unknown instead of null.
    • getMIMEType

      public static String getMIMEType(FileObject fo, String... withinMIMETypes)
      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 return content/unknown instead of null - if you want to avoid such behavior, include null in the list of requested withinMIMETypes - in such case the return value is guaranteed to be one of the values in withinMIMETypes or null.

      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 recognized
      withinMIMETypes - an array of MIME types. Only resolvers whose MIMEResolver#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

      public static void setMIMEType(String extension, String mimeType)
      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 registered
      mimeType - the MIME type to be registered for the extension or null to deregister
      See Also:
    • getMIMETypeExtensions

      public static List<String> getMIMETypeExtensions(String mimeType)
      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 returns null.
      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 public static URLStreamHandler nbfsURLStreamHandler()
      Deprecated.
      No longer used.
    • isParentOf

      public static boolean isParentOf(FileObject folder, FileObject fo)
      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 parent FileObject 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 in
      fo - the file to search for
      Returns:
      true, if fo lies somewhere underneath the folder, false otherwise
      Since:
      3.16
    • isRecursiveSymbolicLink

      public static boolean isRecursiveSymbolicLink(FileObject fo) throws IOException
      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

      public static FileChangeListener weakFileChangeListener(FileChangeListener l, Object source)
      Creates a weak implementation of FileChangeListener.
      Parameters:
      l - the listener to delegate to
      source - the source that the listener should detach from when listener l is freed, can be null
      Returns:
      a FileChangeListener delegating to l.
      Since:
      4.10
    • weakFileStatusListener

      public static FileStatusListener weakFileStatusListener(FileStatusListener l, Object source)
      Creates a weak implementation of FileStatusListener.
      Parameters:
      l - the listener to delegate to
      source - the source that the listener should detach from when listener l is freed, can be null
      Returns:
      a FileChangeListener delegating to l.
      Since:
      4.10
    • getFileDisplayName

      public static String getFileDisplayName(FileObject fo)
      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 passed FileObject isn't root FileObject.isRoot().
      Parameters:
      fo - a file object
      Returns:
      a display name indicating where the file is
      Since:
      4.39
    • normalizePath

      public static String normalizePath(String path)
      Parameters:
      path - file path to normalize
      Returns:
      normalized file path
      Since:
      7.42
    • normalizeFile

      public static File normalizeFile(File file)
      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. Unlike File.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

      public static FileObject getArchiveRoot(FileObject fo)
      Returns a FileObject representing the root folder of an archive. Clients may need to first call isArchiveFile(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

      public static URL getArchiveRoot(URL url)
      Returns a URL representing the root of an archive. Clients may need to first call isArchiveFile(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

      public static FileObject getArchiveFile(FileObject fo)
      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

      public static URL getArchiveFile(URL url)
      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

      public static boolean isArchiveFile(FileObject fo)
      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

      public static boolean isArchiveFile(URL url)
      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

      public static boolean isArchiveArtifact(FileObject fo)
      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

      public static boolean isArchiveArtifact(URL url)
      Tests if an URL denotes a file inside an archive.
      Parameters:
      url - the url to be tested
      Returns:
      true if the url points inside an archive
      Since:
      9.10
    • urlForArchiveOrDir

      public static URL urlForArchiveOrDir(File entry)
      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, a file URL will be created. If it looks to represent a ZIP archive, a jar 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

      public static File archiveOrDirForURL(URL entry)
      Convert a classpath-type URL to a corresponding file. If it is a jar URL representing the root folder of a local disk archive, that archive file will be returned. If it is a file 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 use JFileChooser.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 chooser
      currentDirectory - if not null, a file to set as the current directory using JFileChooser.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 form earlier/later may also be used; if the above attribute has a boolean value of true, then the file named earlier will be sorted somewhere (not necessarily directly) before the file named later. 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 same FileObject.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

      public static void setOrder(List<FileObject> children) throws IllegalArgumentException, IOException
      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 the position 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 same FileObject.getParent()
      Throws:
      IllegalArgumentException - in case there are duplicates, or nulls, or the files do not have a common parent
      IOException - if new file attributes to order the children cannot be written out
      Since:
      org.openide.filesystems 7.2
    • affectsOrder

      public static boolean affectsOrder(FileAttributeEvent event)
      Checks whether a change in a given file attribute would affect the result of getOrder(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

      public static FileObject getConfigFile(String path)
      Returns FileObject from the NetBeans default (system, configuration) filesystem or null if does not exist. If you wish to create the file/folder when it does not already exist, start with getConfigRoot() and use createData(FileObject, String) or createFolder(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 or null if does not exist
      Throws:
      NullPointerException - if the path is null
      Since:
      org.openide.filesystems 7.19, 9.5 support for multiuser environment
    • getSystemConfigFile

      public static FileObject getSystemConfigFile(String path)
      Returns FileObject from the default filesystem, or null if the file does not exist. Unlike getConfigFile(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 to getConfigFile(java.lang.String) to produce singleton services even in multiple context environment.

      With the default Lookup implementation, behaviour of getSystemConfigFile and getConfigFile(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 or null if does not exist
      Throws:
      NullPointerException - if the path is null
      Since:
      9.5
    • getConfigObject

      public static <T> T getConfigObject(String path, Class<T> type)
      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 file
      type - 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

      public static <T> T getSystemConfigObject(String path, Class<T> type)
      Finds a config object under the given path, in system-wide configuration. In the default implementation, this method works just as getConfigObject(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 as getConfigObject(java.lang.String, java.lang.Class<T>) prior version 9.5
      Parameters:
      path - path to .instance or .settings file
      type - the requested type for given object
      Returns:
      either null or instance of requrested type
      Since:
      9.5
    • getConfigRoot

      public static FileObject 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

      public static FileObject getSystemConfigRoot()
      Returns the root of the NetBeans default (system, configuration) filesystem. Unlike getConfigRoot(), 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

      public static boolean isValidFileName(String fileName)
      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