Class FileHelper

java.lang.Object
com.helger.io.file.FileHelper

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

    • isWarnOnIssues

      public static boolean isWarnOnIssues()
      Check if warnings on file operation issues are enabled.
      Returns:
      true if warnings are enabled, false otherwise.
    • setWarnOnIssues

      public static void setWarnOnIssues(boolean bWarnOnIssues)
      Enable or disable warnings on file operation issues.
      Parameters:
      bWarnOnIssues - true to enable warnings, false to disable them.
    • equalFiles

      public static boolean equalFiles(@NonNull File aObj1, @NonNull File aObj2)
      Compare two files by their clean absolute paths for equality.
      Parameters:
      aObj1 - The first file. May not be null.
      aObj2 - The second file. May not be null.
      Returns:
      true if the two files point to the same path, false otherwise.
    • 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
    • ensureParentDirectoryIsPresent

      public static @NonNull com.helger.base.state.EChange ensureParentDirectoryIsPresent(@NonNull File aFile)
      Ensure that the parent directory of the passed file is present. If it is not present, it will be created.
      Parameters:
      aFile - The file whose parent directory should be present. May not be null.
      Returns:
      EChange.CHANGED if the parent directory was created, EChange.UNCHANGED if it already existed.
      Throws:
      IllegalStateException - If the parent directory could not be created.
    • getCanonicalFile

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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

      public static @Nullable 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:
    • getInputStream

      public static @Nullable FileInputStream getInputStream(@NonNull File aFile)
      Get a FileInputStream for the passed file.
      Parameters:
      aFile - The file to open for reading. May not be null.
      Returns:
      null if the file does not exist or cannot be opened.
    • getBufferedInputStream

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedInputStream getBufferedInputStream(@NonNull File aFile)
      Get a buffered input stream for the passed file.
      Parameters:
      aFile - The file to open for reading. May not be null.
      Returns:
      null if the file does not exist or cannot be opened.
    • getReader

      public static @Nullable InputStreamReader getReader(@NonNull File aFile, @NonNull Charset aCharset)
      Get a Reader for the passed file using the specified charset.
      Parameters:
      aFile - The file to open for reading. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file does not exist or cannot be opened.
    • getBufferedReader

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedReader getBufferedReader(@NonNull File aFile, @NonNull Charset aCharset)
      Get a buffered Reader for the passed file using the specified charset.
      Parameters:
      aFile - The file to open for reading. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file does not exist or cannot be opened.
    • getOutputStream

      public static @Nullable 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

      public static @Nullable FileOutputStream getOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.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
    • getBufferedOutputStream

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull File aFile)
      Get a buffered output stream for the passed file using the default append mode.
      Parameters:
      aFile - The file to write to. May not be null.
      Returns:
      null if the file could not be opened.
    • getBufferedOutputStream

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend)
      Get a buffered output stream for the passed 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.
    • getWriter

      public static @Nullable OutputStreamWriter getWriter(@NonNull File aFile, @NonNull Charset aCharset)
      Get a Writer for the passed file using the default append mode and the specified charset.
      Parameters:
      aFile - The file to write to. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getWriter

      public static @Nullable OutputStreamWriter getWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset)
      Get a Writer for the passed file using the specified append mode and charset.
      Parameters:
      aFile - The file to write to. May not be null.
      eAppend - Appending mode. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getBufferedWriter

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWriter getBufferedWriter(@NonNull File aFile, @NonNull Charset aCharset)
      Get a buffered Writer for the passed file using the default append mode and the specified charset.
      Parameters:
      aFile - The file to write to. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getBufferedWriter

      public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWriter getBufferedWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset)
      Get a buffered Writer for the passed file using the specified append mode and charset.
      Parameters:
      aFile - The file to write to. May not be null.
      eAppend - Appending mode. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getPrintWriter

      public static @Nullable PrintWriter getPrintWriter(@NonNull File aFile, @NonNull Charset aCharset)
      Get a PrintWriter for the passed file using the default append mode and the specified charset.
      Parameters:
      aFile - The file to write to. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getPrintWriter

      public static @Nullable PrintWriter getPrintWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset)
      Get a PrintWriter for the passed file using the specified append mode and charset.
      Parameters:
      aFile - The file to write to. May not be null.
      eAppend - Appending mode. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      null if the file could not be opened.
    • getRandomAccessFile

      public static @Nullable RandomAccessFile getRandomAccessFile(@NonNull String sFilename, @NonNull ERandomAccessFileMode eMode)
      Get a RandomAccessFile for the passed filename and mode.
      Parameters:
      sFilename - The name of the file to open. May not be null.
      eMode - The access mode. May not be null.
      Returns:
      null if the file could not be opened.
    • getRandomAccessFile

      public static @Nullable RandomAccessFile getRandomAccessFile(@NonNull File aFile, @NonNull ERandomAccessFileMode eMode)
      Get a RandomAccessFile for the passed file and mode.
      Parameters:
      aFile - The file to open. May not be null.
      eMode - The access 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

      public static @NonNull String getFileSizeDisplay(@Nullable File aFile)
      Get a human-readable file size display string for the passed file using no decimal places.
      Parameters:
      aFile - The file to get the size display for. May be null.
      Returns:
      An empty string if the file is null or does not exist; otherwise a human-readable size string.
    • getFileSizeDisplay

      public static @NonNull String getFileSizeDisplay(@Nullable File aFile, @Nonnegative int nDecimals)
      Get a human-readable file size display string for the passed file.
      Parameters:
      aFile - The file to get the size display for. May be null.
      nDecimals - The number of decimal places to use. Must be ≥ 0.
      Returns:
      An empty string if the file is null or does not exist; otherwise a human-readable size string.
    • getFileSizeDisplay

      public static @NonNull String getFileSizeDisplay(long nFileSize)
      Get a human-readable file size display string for the passed file size using no decimal places.
      Parameters:
      nFileSize - The file size in bytes. Must be ≥ 0.
      Returns:
      A human-readable size string. Never null.
    • getFileSizeDisplay

      public static @NonNull String getFileSizeDisplay(@Nonnegative long nFileSize, @Nonnegative int nDecimals)
      Get a human-readable file size display string for the passed file size.
      Parameters:
      nFileSize - The file size in bytes. Must be ≥ 0.
      nDecimals - The number of decimal places to use. Must be ≥ 0.
      Returns:
      A human-readable size string. Never null.
    • getSecureFile

      public static @Nullable 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:
    • getDirectoryContent

      @ReturnsMutableCopy public static @NonNull com.helger.collection.commons.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

      @ReturnsMutableCopy public static @NonNull com.helger.collection.commons.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

      @ReturnsMutableCopy public static @NonNull com.helger.collection.commons.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.
    • getAsURL

      public static @Nullable URL getAsURL(@NonNull File aFile)
      Convert the passed file to a URL.
      Parameters:
      aFile - The file to be converted. May not be null.
      Returns:
      null if the conversion to URL failed.
    • getAsURLString

      public static @Nullable String getAsURLString(@NonNull File aFile)
      Convert the passed file to a URL string representation.
      Parameters:
      aFile - The file to be converted. May not be null.
      Returns:
      null if the conversion to URL failed.