Class FileHelper


  • @Immutable
    public final class FileHelper
    extends Object
    Miscellaneous file utility methods.
    Author:
    Philip Helger
    • Method Detail

      • existsFile

        public static boolean existsFile​(@Nullable
                                         File aFile)
        Check if the passed file exists. Must be existing and a file.
        Parameters:
        aFile - The file to be checked for existence. May be null .
        Returns:
        true if the passed file is non-null, is a file and exists, false otherwise.
      • existsDir

        public static boolean existsDir​(@Nullable
                                        File aDir)
        Check if the passed directory exists. Must be existing and must be a directory!
        Parameters:
        aDir - The directory to be checked for existence. May be null.
        Returns:
        true if the passed directory is not null, is a directory and exists, false otherwise.
      • canReadAndWriteFile

        public static boolean canReadAndWriteFile​(@Nullable
                                                  File aFile)
        Check if the passed file can read and write. If the file already exists, the file itself is checked. If the file does not exist, the parent directory
        Parameters:
        aFile - The file to be checked. May be null.
        Returns:
        true if the file can be read and written
      • getCanonicalFile

        @Nullable
        public static File getCanonicalFile​(@Nullable
                                            File aFile)
                                     throws IOException
        Get the canonical file of the passed file, if the file is not null.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
        Throws:
        IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
      • getCanonicalFileOrNull

        @Nullable
        public static File getCanonicalFileOrNull​(@Nullable
                                                  File aFile)
        Get the canonical file of the passed file, if the file is not null. In case of an IOException, null is returned.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null or an exception occurred.
      • getCanonicalPath

        @Nullable
        public static String getCanonicalPath​(@Nullable
                                              File aFile)
                                       throws IOException
        Get the canonical path of the passed file, if the file is not null.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
        Throws:
        IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
      • getCanonicalPathOrNull

        @Nullable
        public static String getCanonicalPathOrNull​(@Nullable
                                                    File aFile)
        Get the canonical path of the passed file, if the file is not null. In case of an IOException, null is returned.
        Parameters:
        aFile - The file to get the canonical path from. May be null.
        Returns:
        null if the passed file is null.
      • isParentDirectory

        public static boolean isParentDirectory​(@Nonnull
                                                File aSearchDirectory,
                                                @Nonnull
                                                File aStartDirectory)
        Check if the searched directory is a parent object of the start directory
        Parameters:
        aSearchDirectory - The directory to be searched. May not be null.
        aStartDirectory - The directory where the search starts. May not be null.
        Returns:
        true if the search directory is a parent of the start directory, false otherwise.
        See Also:
        getCanonicalFile(File)
      • getOutputStream

        @Nullable
        public static FileOutputStream getOutputStream​(@Nonnull
                                                       File aFile)
        Get an output stream for writing to a file.
        Parameters:
        aFile - The file to write to. May not be null.
        Returns:
        null if the file could not be opened
      • getOutputStream

        @Nullable
        public static FileOutputStream getOutputStream​(@Nonnull
                                                       File aFile,
                                                       @Nonnull
                                                       EAppend eAppend)
        Get an output stream for writing to a file.
        Parameters:
        aFile - The file to write to. May not be null.
        eAppend - Appending mode. May not be null.
        Returns:
        null if the file could not be opened
      • isFileNewer

        public static boolean isFileNewer​(@Nonnull
                                          File aFile1,
                                          @Nonnull
                                          File aFile2)
        Returns true if the first file is newer than the second file. Returns true if the first file exists and the second file does not exist. Returns false if the first file is older than the second file. Returns false if the first file does not exists but the second does. Returns false if none of the files exist.
        Parameters:
        aFile1 - First file. May not be null.
        aFile2 - Second file. May not be null.
        Returns:
        true if the first file is newer than the second file, false otherwise.
      • getFileSizeDisplay

        @Nonnull
        public static String getFileSizeDisplay​(long nFileSize)
      • getSecureFile

        @Nullable
        public static File getSecureFile​(@Nullable
                                         File aFile)
        Get a secure File object based on the passed file object. First all relative paths ("." and "..") are resolved and all eventually contained '\0' characters are eliminated. Than all file names are checked for validity (so that no special characters are contained).
        Parameters:
        aFile - The file to be secured.
        Returns:
        null if the passed file is null.
      • getDirectoryObjectCount

        @Nonnegative
        public static int getDirectoryObjectCount​(@Nonnull
                                                  File aDirectory)
        Returns the number of files and directories contained in the passed directory excluding the system internal directories.
        Parameters:
        aDirectory - The directory to check. May not be null and must be a directory.
        Returns:
        A non-negative number of objects in that directory.
        See Also:
        FilenameHelper.isSystemInternalDirectory(CharSequence)
      • getDirectoryContent

        @Nonnull
        @ReturnsMutableCopy
        public static ICommonsList<File> getDirectoryContent​(@Nonnull
                                                             File aDirectory)
        This is a replacement for File.listFiles() doing some additional checks on permissions. The order of the returned files is defined by the underlying File.listFiles() method.
        Parameters:
        aDirectory - The directory to be listed. May not be null.
        Returns:
        Never null.
      • getDirectoryContent

        @Nonnull
        @ReturnsMutableCopy
        public static ICommonsList<File> getDirectoryContent​(@Nonnull
                                                             File aDirectory,
                                                             @Nonnull
                                                             FilenameFilter aFilenameFilter)
        This is a replacement for File.listFiles(FilenameFilter) doing some additional checks on permissions. The order of the returned files is defined by the underlying File.listFiles(FilenameFilter) method.
        Parameters:
        aDirectory - The directory to be listed. May not be null.
        aFilenameFilter - The filename filter to be used. May not be null.
        Returns:
        Never null.
      • getDirectoryContent

        @Nonnull
        @ReturnsMutableCopy
        public static ICommonsList<File> getDirectoryContent​(@Nonnull
                                                             File aDirectory,
                                                             @Nonnull
                                                             FileFilter aFileFilter)
        This is a replacement for File.listFiles(FileFilter) doing some additional checks on permissions. The order of the returned files is defined by the underlying File.listFiles(FileFilter) method.
        Parameters:
        aDirectory - The directory to be listed. May not be null.
        aFileFilter - The file filter to be used. May not be null.
        Returns:
        Never null.