Package info.freelibrary.util
Class FileUtils
- java.lang.Object
-
- info.freelibrary.util.FileUtils
-
public final class FileUtils extends Object
Utilities for working with files.
-
-
Field Summary
Fields Modifier and Type Field Description static String
DATE_FORMAT
A date format pattern.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
convertToInt(Set<PosixFilePermission> aPermSet)
Convert a PosixFilePermission set to an integer permissions mode.static Set<PosixFilePermission>
convertToPermissionsSet(int aMode)
Converts an file permissions mode integer to a PosixFilePermission set.static void
copy(File aFromFile, File aToFile)
Copies a file or directory from one place to another.static boolean
delete(File aDir)
Deletes a directory and all its children.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').static String
getExt(String aFileName)
Returns a file extension (as delimited by a period).static String
getMimeType(String aFileUrl)
Gets the MIME type of the supplied file URL.static long
getSize(File aFile)
Gets the calculated size of a directory of files.static String
hash(File aFile, String aAlgorithm)
Get a hash for a supplied file.static File[]
listFiles(File aDir, FilenameFilter aFilter)
An array of all the files in the supplied directory that match the suppliedFilenameFilter
.static File[]
listFiles(File aDir, FilenameFilter aFilter, boolean aDeepListing)
An array of all the files in the supplied directory that match the suppliedFilenameFilter
.static File[]
listFiles(File aDir, FilenameFilter aFilter, boolean aDeepListing, String... aIgnoreList)
An array of all the files in the supplied directory that match the suppliedFilenameFilter
.static String
sizeFromBytes(long aByteCount)
Returns a human readable size from a large number of bytes.static String
sizeFromBytes(long aByteCount, boolean aAbbreviatedLabel)
Returns a human readable size from a large number of bytes.static String
stripExt(File aFile)
Return a file name without the dot extension.static String
stripExt(String aFileName)
Return a file name without the dot extension.static File
toFile(URL aURL)
Returns a JavaFile
for the supplied file-based URL.static Map<String,List<String>>
toHashMap(String aFilePath)
Returns a Map representation of the supplied directory's structure.static Map<String,List<String>>
toHashMap(String aFilePath, String aPattern)
Returns a Map representation of the supplied directory's structure.static Map<String,List<String>>
toHashMap(String aFilePath, String aPattern, String... aIgnoreList)
Returns a Map representation of the supplied directory's structure.
-
-
-
Field Detail
-
DATE_FORMAT
public static final String DATE_FORMAT
A date format pattern.- See Also:
- Constant Field Values
-
-
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 listingaPattern
- 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 listingaPattern
- A regular expression pattern which the files must match to be returnedaIgnoreList
- 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 existRuntimeException
- If a duplicate file path name is discovered
-
toFile
public static File toFile(URL aURL) throws MalformedURLException
Returns a JavaFile
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 suppliedFilenameFilter
.- Parameters:
aDir
- A directory from which a file listing should be returnedaFilter
- 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 suppliedFilenameFilter
.- Parameters:
aDir
- A directory from which a file listing should be returnedaFilter
- A file name filter which returned files should matchaDeepListing
- 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 suppliedFilenameFilter
. 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 returnedaFilter
- A file name filter which returned files should matchaDeepListing
- Whether we should descend through subdirectoriesaIgnoreList
- 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 sourceaToFile
- 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 bytesaAbbreviatedLabel
- Whether the label should be abbreviated- Returns:
- A human readable size
-
hash
public static String hash(File aFile, String aAlgorithm) throws NoSuchAlgorithmException, IOException
Get a hash for a supplied file.- Parameters:
aFile
- A file to get a hash foraAlgorithm
- A hash algorithm supported byMessageDigest
- Returns:
- A hash string
- Throws:
NoSuchAlgorithmException
- If the supplied algorithm isn't supportedIOException
- If there is trouble reading the file
-
getMimeType
public static String getMimeType(String aFileUrl)
Gets the MIME type of the supplied file URL. It gets MIME type fromURLConnection
'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 systemaPermString
- 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
-
-