Class FileUtilities
Various static methods helpful for handling files.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final Stringdigest(InputStream in, String algorithm) Return a message digest of an InputStream.static final StringReturn a message digest of a file.getCanonicalFileNames(ArrayList<File> files) static final FileDetermine if a file corresponding to the specified name exists, checking for case insensitive variants if necessary.getFilePathComponents(File path) Get the individual components of the canonical form of the path as a list.listFilesRecursively(File initialPath) Recursively traverse the specified directory and its sub-directory and produce a list of all the files contained therein, in no particular order.static final StringmakePathToFileInUsersHomeDirectory(String fileName) Given a file name, such as the properties file name, make a path to it in the user's home directory.static FilemakeSameRelativePathNameInDifferentFolder(File srcFolder, File dstFolder, File srcFile) Create a new path that re-creates the relative path of the source file in the destination folder.static FilemakeSameRelativePathNameInDifferentFolder(String srcFolderName, String dstFolderName, String srcFileName) Create a new path that re-creates the relative path of the source file in the destination folder.static final StringCreate a temporary filename.static final Stringmd5(InputStream in) Return an MD5 message digest of an InputStream.static final StringReturn an MD5 message digest of a file.static byte[]readAllBytes(InputStream stream) Read an entire input stream, such as from a resource in a jar file, into an array of bytes.static final StringRead an entire file into a string.static final StringreadFile(InputStream stream) Read an entire file into a string.static final StringRead an entire file into a string.static final StringRead an entire file into a string.static final voidrenameElseCopyTo(File srcFile, File dstFile) Rename a file, if possible, else make a copy of it.
-
Method Details
-
renameElseCopyTo
Rename a file, if possible, else make a copy of it.
- Parameters:
srcFile- the sourcedstFile- the destination- Throws:
IOException- thrown if the copying fails for any reason
-
listFilesRecursively
Recursively traverse the specified directory and its sub-directory and produce a list of all the files contained therein, in no particular order.
If the path is a file, just return that.
Any security (permission) exceptions are caught and logged to stderr and not propagated.
- Parameters:
initialPath- The abstract pathname of the directory to begin searching- Returns:
- An ArrayList of abstract pathnames denoting the files found. The ArrayList will be empty if the path is empty or does not exist or if an error occurs.
-
getCanonicalFileNames
public static final ArrayList<String> getCanonicalFileNames(ArrayList<File> files) throws IOException - Throws:
IOException
-
readFile
Read an entire file into a string.
- Parameters:
reader- The file reader- Returns:
- The contents of the file as a
String. - Throws:
IOException- If an IO error occurs.
-
readFile
Read an entire file into a string.
- Parameters:
stream- The input stream (e.g., fromclass.getResourceAsStream())- Returns:
- The contents of the file as a
String. - Throws:
IOException- If an IO error occurs.
-
readFile
Read an entire file into a string.
- Parameters:
file- The file- Returns:
- The contents of the file as a
String. - Throws:
IOException- If an IO error occurs.
-
readFile
Read an entire file into a string.
- Parameters:
filename- The file- Returns:
- The contents of the file as a
String. - Throws:
IOException- If an IO error occurs.
-
readAllBytes
Read an entire input stream, such as from a resource in a jar file, into an array of bytes.
Modelled after Files.readAllBytes(InputStream(Path path), from which the following description is paraphrased:
Reads all the bytes from an input stream. The method ensures that the input stream is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown.
Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading in large input streams.
- Parameters:
stream- The input stream (e.g., fromclass.getResourceAsStream())- Returns:
- The contents of the stream
- Throws:
IOException- If an IO error occurs.
-
getFileFromNameInsensitiveToCaseIfNecessary
public static final File getFileFromNameInsensitiveToCaseIfNecessary(String fileName) throws FileNotFoundException Determine if a file corresponding to the specified name exists, checking for case insensitive variants if necessary.
- Parameters:
fileName- The name of the file to find- Returns:
- A file if found.
- Throws:
FileNotFoundException- If the file cannot be found.
-
makeTemporaryFileName
Create a temporary filename.
- Returns:
- a string that does not include delimiter characters and is unique within this JVM.
-
makePathToFileInUsersHomeDirectory
Given a file name, such as the properties file name, make a path to it in the user's home directory.
- Parameters:
fileName- the file name to make a path to
-
digest
public static final String digest(String fileName, String algorithm) throws IOException, NoSuchAlgorithmException Return a message digest of a file.
- Parameters:
fileName- the file namealgorithm- the digest algorithm, such as "MD5" or "SHA"- Returns:
- string representation of the digest
- Throws:
IOExceptionNoSuchAlgorithmException
-
digest
public static final String digest(InputStream in, String algorithm) throws IOException, NoSuchAlgorithmException Return a message digest of an InputStream.
- Parameters:
in- the InputStreamalgorithm- the digest algorithm, such as "MD5" or "SHA"- Returns:
- string representation of the digest
- Throws:
IOExceptionNoSuchAlgorithmException
-
md5
Return an MD5 message digest of a file.
- Parameters:
fileName- the file name- Returns:
- string representation of the digest
- Throws:
IOExceptionNoSuchAlgorithmException
-
md5
Return an MD5 message digest of an InputStream.
- Parameters:
in- the InputStream- Returns:
- string representation of the digest
- Throws:
IOExceptionNoSuchAlgorithmException
-
getFilePathComponents
Get the individual components of the canonical form of the path as a list.
- Parameters:
path-- Returns:
- each component of the path, starting with the root
- Throws:
IOException
-
makeSameRelativePathNameInDifferentFolder
public static File makeSameRelativePathNameInDifferentFolder(String srcFolderName, String dstFolderName, String srcFileName) throws IOException Create a new path that re-creates the relative path of the source file in the destination folder.
- Parameters:
srcFolderName-dstFolderName-srcFileName-- Returns:
- a File in the destination folder
- Throws:
IOException
-
makeSameRelativePathNameInDifferentFolder
public static File makeSameRelativePathNameInDifferentFolder(File srcFolder, File dstFolder, File srcFile) throws IOException Create a new path that re-creates the relative path of the source file in the destination folder.
- Parameters:
srcFolder-dstFolder-srcFile-- Returns:
- a File in the destination folder
- Throws:
IOException
-