Class FilePath

  • Direct Known Subclasses:
    FilePathDisk, FilePathMem, FilePathNioMem, FilePathWrapper, FilePathZip

    public abstract class FilePath
    extends java.lang.Object
    A path to a file. It similar to the Java 7 java.nio.file.Path, but simpler, and works with older versions of Java. It also implements the relevant methods found in java.nio.file.FileSystem and FileSystems
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static FilePath defaultProvider  
      java.lang.String name
      The complete path (which may be absolute or relative, depending on the file system).
      private static java.util.concurrent.ConcurrentHashMap<java.lang.String,​FilePath> providers  
      private static java.lang.String tempRandom
      The prefix for temporary files.
      private static long tempSequence  
    • Constructor Summary

      Constructors 
      Constructor Description
      FilePath()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract boolean canWrite()
      Check if the file is writable.
      abstract void createDirectory()
      Create a directory (all required parent directories already exist).
      abstract boolean createFile()
      Create a new file.
      FilePath createTempFile​(java.lang.String suffix, boolean inTempDir)
      Create a new temporary file.
      abstract void delete()
      Delete a file or directory if it exists.
      abstract boolean exists()
      Checks if a file exists.
      static FilePath get​(java.lang.String path)
      Get the file path object for the given path.
      java.lang.String getName()
      Get the file or directory name (the last element of the path).
      private static java.lang.String getNextTempFileNamePart​(boolean newRandom)
      Get the next temporary file name part (the part in the middle).
      abstract FilePath getParent()
      Get the parent directory of a file or directory.
      abstract FilePath getPath​(java.lang.String path)
      Convert a file to a path.
      abstract java.lang.String getScheme()
      Get the scheme (prefix) for this file provider.
      abstract boolean isAbsolute()
      Check if the file name includes a path.
      abstract boolean isDirectory()
      Check if it is a file or a directory.
      abstract long lastModified()
      Get the last modified date of a file
      abstract void moveTo​(FilePath newName, boolean atomicReplace)
      Rename a file if this is allowed.
      abstract java.util.List<FilePath> newDirectoryStream()
      List the files and directories in the given directory.
      static java.io.OutputStream newFileChannelOutputStream​(java.nio.channels.FileChannel channel, boolean append)
      Create a new output stream from the channel.
      java.io.InputStream newInputStream()
      Create an input stream to read from the file.
      java.io.OutputStream newOutputStream​(boolean append)
      Create an output stream to write into the file.
      abstract java.nio.channels.FileChannel open​(java.lang.String mode)
      Open a random access file object.
      static void register​(FilePath provider)
      Register a file provider.
      abstract boolean setReadOnly()
      Disable the ability to write.
      abstract long size()
      Get the size of a file in bytes
      abstract FilePath toRealPath()
      Normalize a file name.
      java.lang.String toString()
      Get the string representation.
      static void unregister​(FilePath provider)
      Unregister a file provider.
      FilePath unwrap()
      Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • defaultProvider

        private static final FilePath defaultProvider
      • providers

        private static final java.util.concurrent.ConcurrentHashMap<java.lang.String,​FilePath> providers
      • tempRandom

        private static java.lang.String tempRandom
        The prefix for temporary files.
      • tempSequence

        private static long tempSequence
      • name

        public java.lang.String name
        The complete path (which may be absolute or relative, depending on the file system).
    • Constructor Detail

      • FilePath

        public FilePath()
    • Method Detail

      • get

        public static FilePath get​(java.lang.String path)
        Get the file path object for the given path. Windows-style '\' is replaced with '/'.
        Parameters:
        path - the path
        Returns:
        the file path object
      • register

        public static void register​(FilePath provider)
        Register a file provider.
        Parameters:
        provider - the file provider
      • unregister

        public static void unregister​(FilePath provider)
        Unregister a file provider.
        Parameters:
        provider - the file provider
      • size

        public abstract long size()
        Get the size of a file in bytes
        Returns:
        the size in bytes
      • moveTo

        public abstract void moveTo​(FilePath newName,
                                    boolean atomicReplace)
        Rename a file if this is allowed.
        Parameters:
        newName - the new fully qualified file name
        atomicReplace - whether the move should be atomic, and the target file should be replaced if it exists and replacing is possible
      • createFile

        public abstract boolean createFile()
        Create a new file.
        Returns:
        true if creating was successful
      • exists

        public abstract boolean exists()
        Checks if a file exists.
        Returns:
        true if it exists
      • delete

        public abstract void delete()
        Delete a file or directory if it exists. Directories may only be deleted if they are empty.
      • newDirectoryStream

        public abstract java.util.List<FilePath> newDirectoryStream()
        List the files and directories in the given directory.
        Returns:
        the list of fully qualified file names
      • toRealPath

        public abstract FilePath toRealPath()
        Normalize a file name.
        Returns:
        the normalized file name
      • getParent

        public abstract FilePath getParent()
        Get the parent directory of a file or directory.
        Returns:
        the parent directory name
      • isDirectory

        public abstract boolean isDirectory()
        Check if it is a file or a directory.
        Returns:
        true if it is a directory
      • isAbsolute

        public abstract boolean isAbsolute()
        Check if the file name includes a path.
        Returns:
        if the file name is absolute
      • lastModified

        public abstract long lastModified()
        Get the last modified date of a file
        Returns:
        the last modified date
      • canWrite

        public abstract boolean canWrite()
        Check if the file is writable.
        Returns:
        if the file is writable
      • createDirectory

        public abstract void createDirectory()
        Create a directory (all required parent directories already exist).
      • getName

        public java.lang.String getName()
        Get the file or directory name (the last element of the path).
        Returns:
        the last element of the path
      • newOutputStream

        public java.io.OutputStream newOutputStream​(boolean append)
                                             throws java.io.IOException
        Create an output stream to write into the file.
        Parameters:
        append - if true, the file will grow, if false, the file will be truncated first
        Returns:
        the output stream
        Throws:
        java.io.IOException - If an I/O error occurs
      • newFileChannelOutputStream

        public static final java.io.OutputStream newFileChannelOutputStream​(java.nio.channels.FileChannel channel,
                                                                            boolean append)
                                                                     throws java.io.IOException
        Create a new output stream from the channel.
        Parameters:
        channel - the file channel
        append - true for append mode, false for truncate and overwrite
        Returns:
        the output stream
        Throws:
        java.io.IOException - on I/O exception
      • open

        public abstract java.nio.channels.FileChannel open​(java.lang.String mode)
                                                    throws java.io.IOException
        Open a random access file object.
        Parameters:
        mode - the access mode. Supported are r, rw, rws, rwd
        Returns:
        the file object
        Throws:
        java.io.IOException - If an I/O error occurs
      • newInputStream

        public java.io.InputStream newInputStream()
                                           throws java.io.IOException
        Create an input stream to read from the file.
        Returns:
        the input stream
        Throws:
        java.io.IOException - If an I/O error occurs
      • setReadOnly

        public abstract boolean setReadOnly()
        Disable the ability to write.
        Returns:
        true if the call was successful
      • createTempFile

        public FilePath createTempFile​(java.lang.String suffix,
                                       boolean inTempDir)
                                throws java.io.IOException
        Create a new temporary file.
        Parameters:
        suffix - the suffix
        inTempDir - if the file should be stored in the temporary directory
        Returns:
        the name of the created file
        Throws:
        java.io.IOException - on failure
      • getNextTempFileNamePart

        private static java.lang.String getNextTempFileNamePart​(boolean newRandom)
        Get the next temporary file name part (the part in the middle).
        Parameters:
        newRandom - if the random part of the filename should change
        Returns:
        the file name part
      • toString

        public java.lang.String toString()
        Get the string representation. The returned string can be used to construct a new object.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the path as a string
      • getScheme

        public abstract java.lang.String getScheme()
        Get the scheme (prefix) for this file provider. This is similar to java.nio.file.spi.FileSystemProvider.getScheme.
        Returns:
        the scheme
      • getPath

        public abstract FilePath getPath​(java.lang.String path)
        Convert a file to a path. This is similar to java.nio.file.spi.FileSystemProvider.getPath, but may return an object even if the scheme doesn't match in case of the default file provider.
        Parameters:
        path - the path
        Returns:
        the file path object
      • unwrap

        public FilePath unwrap()
        Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
        Returns:
        the unwrapped path