public class FileUtilities
extends java.lang.Object
Various static methods helpful for handling files.
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
digest(java.io.InputStream in,
java.lang.String algorithm)
Return a message digest of an InputStream.
|
static java.lang.String |
digest(java.lang.String fileName,
java.lang.String algorithm)
Return a message digest of a file.
|
static java.util.ArrayList<java.lang.String> |
getCanonicalFileNames(java.util.ArrayList<java.io.File> files) |
static java.io.File |
getFileFromNameInsensitiveToCaseIfNecessary(java.lang.String fileName)
Determine if a file corresponding to the specified name exists,
checking for case insensitive variants if necessary.
|
static java.util.List<java.lang.String> |
getFilePathComponents(java.io.File path)
Get the individual components of the canonical form of the path as a list.
|
static java.util.ArrayList<java.io.File> |
listFilesRecursively(java.io.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 java.lang.String |
makePathToFileInUsersHomeDirectory(java.lang.String fileName)
Given a file name, such as the properties file name, make a path to it in the user's home directory.
|
static java.io.File |
makeSameRelativePathNameInDifferentFolder(java.io.File srcFolder,
java.io.File dstFolder,
java.io.File srcFile)
Create a new path that re-creates the relative path of the source file in the destination folder.
|
static java.io.File |
makeSameRelativePathNameInDifferentFolder(java.lang.String srcFolderName,
java.lang.String dstFolderName,
java.lang.String srcFileName)
Create a new path that re-creates the relative path of the source file in the destination folder.
|
static java.lang.String |
makeTemporaryFileName()
Create a temporary filename.
|
static java.lang.String |
md5(java.io.InputStream in)
Return an MD5 message digest of an InputStream.
|
static java.lang.String |
md5(java.lang.String fileName)
Return an MD5 message digest of a file.
|
static byte[] |
readAllBytes(java.io.InputStream stream)
Read an entire input stream, such as from a resource in a jar file, into an array of bytes.
|
static java.lang.String |
readFile(java.io.File file)
Read an entire file into a string.
|
static java.lang.String |
readFile(java.io.InputStream stream)
Read an entire file into a string.
|
static java.lang.String |
readFile(java.io.Reader reader)
Read an entire file into a string.
|
static java.lang.String |
readFile(java.lang.String filename)
Read an entire file into a string.
|
static void |
renameElseCopyTo(java.io.File srcFile,
java.io.File dstFile)
Rename a file, if possible, else make a copy of it.
|
public static final java.lang.String digest(java.io.InputStream in, java.lang.String algorithm) throws java.io.IOException, java.security.NoSuchAlgorithmException
Return a message digest of an InputStream.
in
- the InputStreamalgorithm
- the digest algorithm, such as "MD5" or "SHA"java.io.IOException
java.security.NoSuchAlgorithmException
public static final java.lang.String digest(java.lang.String fileName, java.lang.String algorithm) throws java.io.IOException, java.security.NoSuchAlgorithmException
Return a message digest of a file.
fileName
- the file namealgorithm
- the digest algorithm, such as "MD5" or "SHA"java.io.IOException
java.security.NoSuchAlgorithmException
public static final java.util.ArrayList<java.lang.String> getCanonicalFileNames(java.util.ArrayList<java.io.File> files) throws java.io.IOException
java.io.IOException
public static final java.io.File getFileFromNameInsensitiveToCaseIfNecessary(java.lang.String fileName) throws java.io.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 findjava.io.FileNotFoundException
- If the file cannot be found.public static java.util.List<java.lang.String> getFilePathComponents(java.io.File path) throws java.io.IOException
Get the individual components of the canonical form of the path as a list.
path
- java.io.IOException
public static final java.util.ArrayList<java.io.File> listFilesRecursively(java.io.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 java.lang.String makePathToFileInUsersHomeDirectory(java.lang.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 java.io.File makeSameRelativePathNameInDifferentFolder(java.io.File srcFolder, java.io.File dstFolder, java.io.File srcFile) throws java.io.IOException
Create a new path that re-creates the relative path of the source file in the destination folder.
srcFolder
- dstFolder
- srcFile
- java.io.IOException
public static java.io.File makeSameRelativePathNameInDifferentFolder(java.lang.String srcFolderName, java.lang.String dstFolderName, java.lang.String srcFileName) throws java.io.IOException
Create a new path that re-creates the relative path of the source file in the destination folder.
srcFolderName
- dstFolderName
- srcFileName
- java.io.IOException
public static final java.lang.String makeTemporaryFileName()
Create a temporary filename.
public static final java.lang.String md5(java.io.InputStream in) throws java.io.IOException, java.security.NoSuchAlgorithmException
Return an MD5 message digest of an InputStream.
in
- the InputStreamjava.io.IOException
java.security.NoSuchAlgorithmException
public static final java.lang.String md5(java.lang.String fileName) throws java.io.IOException, java.security.NoSuchAlgorithmException
Return an MD5 message digest of a file.
fileName
- the file namejava.io.IOException
java.security.NoSuchAlgorithmException
public static byte[] readAllBytes(java.io.InputStream stream) throws java.io.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()
)java.io.IOException
- If an IO error occurs.public static final java.lang.String readFile(java.io.File file) throws java.io.IOException
Read an entire file into a string.
file
- The fileString
.java.io.IOException
- If an IO error occurs.public static final java.lang.String readFile(java.io.InputStream stream) throws java.io.IOException
Read an entire file into a string.
stream
- The input stream (e.g., from class.getResourceAsStream()
)String
.java.io.IOException
- If an IO error occurs.public static final java.lang.String readFile(java.io.Reader reader) throws java.io.IOException
Read an entire file into a string.
reader
- The file readerString
.java.io.IOException
- If an IO error occurs.public static final java.lang.String readFile(java.lang.String filename) throws java.io.IOException
Read an entire file into a string.
filename
- The fileString
.java.io.IOException
- If an IO error occurs.public static final void renameElseCopyTo(java.io.File srcFile, java.io.File dstFile) throws java.io.IOException
Rename a file, if possible, else make a copy of it.
srcFile
- the sourcedstFile
- the destinationjava.io.IOException
- thrown if the copying fails for any reason