public class FileUtilities extends Object
Various static methods helpful for handling files.
| Modifier and Type | Method and Description |
|---|---|
static String |
digest(InputStream in,
String algorithm)
Return a message digest of an InputStream.
|
static String |
digest(String fileName,
String algorithm)
Return a message digest of a file.
|
static ArrayList<String> |
getCanonicalFileNames(ArrayList<File> files) |
static File |
getFileFromNameInsensitiveToCaseIfNecessary(String fileName)
Determine if a file corresponding to the specified name exists,
checking for case insensitive variants if necessary.
|
static List<String> |
getFilePathComponents(File path)
Get the individual components of the canonical form of the path as a list.
|
static ArrayList<File> |
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 String |
makePathToFileInUsersHomeDirectory(String fileName)
Given a file name, such as the properties file name, make a path to it in the user's home directory.
|
static File |
makeSameRelativePathNameInDifferentFolder(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 File |
makeSameRelativePathNameInDifferentFolder(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 String |
makeTemporaryFileName()
Create a temporary filename.
|
static String |
md5(InputStream in)
Return an MD5 message digest of an InputStream.
|
static String |
md5(String fileName)
Return 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 String |
readFile(File file)
Read an entire file into a string.
|
static String |
readFile(InputStream stream)
Read an entire file into a string.
|
static String |
readFile(Reader reader)
Read an entire file into a string.
|
static String |
readFile(String filename)
Read an entire file into a string.
|
static void |
renameElseCopyTo(File srcFile,
File dstFile)
Rename a file, if possible, else make a copy of it.
|
public static final String digest(InputStream in, String algorithm) throws IOException, NoSuchAlgorithmException
Return a message digest of an InputStream.
in - the InputStreamalgorithm - the digest algorithm, such as "MD5" or "SHA"IOExceptionNoSuchAlgorithmExceptionpublic static final String digest(String fileName, String algorithm) throws IOException, NoSuchAlgorithmException
Return a message digest of a file.
fileName - the file namealgorithm - the digest algorithm, such as "MD5" or "SHA"IOExceptionNoSuchAlgorithmExceptionpublic static final ArrayList<String> getCanonicalFileNames(ArrayList<File> files) throws IOException
IOExceptionpublic 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.
fileName - The name of the file to findFileNotFoundException - If the file cannot be found.public static List<String> getFilePathComponents(File path) throws IOException
Get the individual components of the canonical form of the path as a list.
path - IOExceptionpublic static final ArrayList<File> 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.
If the path is a file, just return that.
Any security (permission) exceptions are caught and logged to stderr and not propagated.
initialPath - The abstract pathname of the directory to begin searchingpublic static final String makePathToFileInUsersHomeDirectory(String fileName)
Given a file name, such as the properties file name, make a path to it in the user's home directory.
fileName - the file name to make a path topublic 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.
srcFolder - dstFolder - srcFile - IOExceptionpublic 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.
srcFolderName - dstFolderName - srcFileName - IOExceptionpublic static final String makeTemporaryFileName()
Create a temporary filename.
public static final String md5(InputStream in) throws IOException, NoSuchAlgorithmException
Return an MD5 message digest of an InputStream.
in - the InputStreamIOExceptionNoSuchAlgorithmExceptionpublic static final String md5(String fileName) throws IOException, NoSuchAlgorithmException
Return an MD5 message digest of a file.
fileName - the file nameIOExceptionNoSuchAlgorithmExceptionpublic static byte[] readAllBytes(InputStream stream) throws IOException
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.
stream - The input stream (e.g., from class.getResourceAsStream())IOException - If an IO error occurs.public static final String readFile(File file) throws IOException
Read an entire file into a string.
file - The fileString.IOException - If an IO error occurs.public static final String readFile(InputStream stream) throws IOException
Read an entire file into a string.
stream - The input stream (e.g., from class.getResourceAsStream())String.IOException - If an IO error occurs.public static final String readFile(Reader reader) throws IOException
Read an entire file into a string.
reader - The file readerString.IOException - If an IO error occurs.public static final String readFile(String filename) throws IOException
Read an entire file into a string.
filename - The fileString.IOException - If an IO error occurs.public static final void renameElseCopyTo(File srcFile, File dstFile) throws IOException
Rename a file, if possible, else make a copy of it.
srcFile - the sourcedstFile - the destinationIOException - thrown if the copying fails for any reason