Class FileUtils


  • public final class FileUtils
    extends Object
    Utilities for working with files.
    • Method Detail

      • toHashMap

        public static Map<String,​List<String>> toHashMap​(String aFilePath)
                                                        throws FileNotFoundException
        Returns a Map representation of the supplied directory's structure. The map contains the file name as the key and its path as the value. If a file with a name occurs more than once, multiple path values are returned for that file name key. The map that is returned is unmodifiable.
        Parameters:
        aFilePath - The directory of which you'd like a file listing
        Returns:
        An unmodifiable map representing the files in the file structure
        Throws:
        FileNotFoundException - If the directory for the supplied file path does not exist
      • toHashMap

        public static Map<String,​List<String>> toHashMap​(String aFilePath,
                                                               String aPattern)
                                                        throws FileNotFoundException
        Returns a Map representation of the supplied directory's structure. The map contains the file name as the key and its path as the value. If a file with a name occurs more than once, multiple path values are returned for that file name key. The map that is returned is unmodifiable.
        Parameters:
        aFilePath - The directory of which you'd like a file listing
        aPattern - A regular expression pattern which the files must match to be returned
        Returns:
        An unmodifiable map representing the files in the file structure
        Throws:
        FileNotFoundException - If the directory for the supplied file path does not exist
      • toHashMap

        public static Map<String,​List<String>> toHashMap​(String aFilePath,
                                                               String aPattern,
                                                               String... aIgnoreList)
                                                        throws FileNotFoundException
        Returns a Map representation of the supplied directory's structure. The map contains the file name as the key and its path as the value. If a file with a name occurs more than once, multiple path values are returned for that file name key. The map that is returned is unmodifiable.
        Parameters:
        aFilePath - The directory of which you'd like a file listing
        aPattern - A regular expression pattern which the files must match to be returned
        aIgnoreList - A list of directories into which we shouldn't descend
        Returns:
        An unmodifiable map representing the files in the file structure
        Throws:
        FileNotFoundException - If the directory for the supplied file path does not exist
        RuntimeException - If a duplicate file path name is discovered
      • toFile

        public static File toFile​(URL aURL)
                           throws MalformedURLException
        Returns a Java File for the supplied file-based URL.
        Parameters:
        aURL - A URL that has a file protocol
        Returns:
        A Java File for the supplied URL
        Throws:
        MalformedURLException - If the supplied URL doesn't have a file protocol
      • listFiles

        public static File[] listFiles​(File aDir,
                                       FilenameFilter aFilter)
                                throws FileNotFoundException
        An array of all the files in the supplied directory that match the supplied FilenameFilter.
        Parameters:
        aDir - A directory from which a file listing should be returned
        aFilter - A file name filter which returned files should match
        Returns:
        An array of matching files
        Throws:
        FileNotFoundException - If the supplied directory doesn't exist
      • listFiles

        public static File[] listFiles​(File aDir,
                                       FilenameFilter aFilter,
                                       boolean aDeepListing)
                                throws FileNotFoundException
        An array of all the files in the supplied directory that match the supplied FilenameFilter.
        Parameters:
        aDir - A directory from which a file listing should be returned
        aFilter - A file name filter which returned files should match
        aDeepListing - Whether we should descend through subdirectories
        Returns:
        An array of matching files
        Throws:
        FileNotFoundException - If the supplied directory doesn't exist
      • listFiles

        public static File[] listFiles​(File aDir,
                                       FilenameFilter aFilter,
                                       boolean aDeepListing,
                                       String... aIgnoreList)
                                throws FileNotFoundException
        An array of all the files in the supplied directory that match the supplied FilenameFilter. Directories that match the supplied ignore list will not be included in the result.
        Parameters:
        aDir - A directory from which a file listing should be returned
        aFilter - A file name filter which returned files should match
        aDeepListing - Whether we should descend through subdirectories
        aIgnoreList - Directory names that should be ignored in the list.
        Returns:
        An array of matching files
        Throws:
        FileNotFoundException - If the supplied directory doesn't exist
      • stripExt

        public static String stripExt​(File aFile)
        Return a file name without the dot extension.
        Parameters:
        aFile - The file name
        Returns:
        The file name without the extension
      • stripExt

        public static String stripExt​(String aFileName)
        Return a file name without the dot extension.
        Parameters:
        aFileName - The file name from which we want to strip the extension
        Returns:
        The file name without the extension
      • getExt

        public static String getExt​(String aFileName)
        Returns a file extension (as delimited by a period).
        Parameters:
        aFileName - A file name from which to get the extension
        Returns:
        The extension or an empty string if the file doesn't have an extension
      • getSize

        public static long getSize​(File aFile)
        Gets the calculated size of a directory of files.
        Parameters:
        aFile - A file or directory from which to calculate size
        Returns:
        The calculated size of the supplied directory or file
      • delete

        public static boolean delete​(File aDir)
        Deletes a directory and all its children.
        Parameters:
        aDir - A directory to delete
        Returns:
        True if file was successfully deleted; else, false
      • copy

        public static void copy​(File aFromFile,
                                File aToFile)
                         throws IOException
        Copies a file or directory from one place to another. This copies a file to a file or a directory to a directory. It does not copy a file to a directory.
        Parameters:
        aFromFile - A file or directory source
        aToFile - A file or directory destination
        Throws:
        IOException - If there is an exception copying the files or directories
      • sizeFromBytes

        public static String sizeFromBytes​(long aByteCount)
        Returns a human readable size from a large number of bytes.
        Parameters:
        aByteCount - A large number of bytes
        Returns:
        A human readable size
      • sizeFromBytes

        public static String sizeFromBytes​(long aByteCount,
                                           boolean aAbbreviatedLabel)
        Returns a human readable size from a large number of bytes. You can specify that the human readable size use an abbreviated label (e.g., GB or MB).
        Parameters:
        aByteCount - A large number of bytes
        aAbbreviatedLabel - Whether the label should be abbreviated
        Returns:
        A human readable size
      • getMimeType

        public static String getMimeType​(String aFileUrl)
        Gets the MIME type of the supplied file URL. It gets MIME type from URLConnection's file name map.
        Parameters:
        aFileUrl - A file-based URL
        Returns:
        The MIME-type name for the supplied file
      • dirIsUseable

        public static boolean dirIsUseable​(String aDirName,
                                           String aPermString)
        Tests that the supplied directory exists and can be used according to the supplied permissions string (e.g., 'rwx').
        Parameters:
        aDirName - A name of a directory on the file system
        aPermString - A string representing the desired permissions of the directory
        Returns:
        True if the directory is okay to be used; else, false
      • convertToPermissionsSet

        public static Set<PosixFilePermission> convertToPermissionsSet​(int aMode)
        Converts an file permissions mode integer to a PosixFilePermission set.
        Parameters:
        aMode - An integer permissions mode.
        Returns:
        A PosixFilePermission set
      • convertToInt

        public static int convertToInt​(Set<PosixFilePermission> aPermSet)
        Convert a PosixFilePermission set to an integer permissions mode.
        Parameters:
        aPermSet - A PosixFilePermission set
        Returns:
        A permissions mode integer