Package com.helger.io.file
Class FileHelper
java.lang.Object
com.helger.io.file.FileHelper
Miscellaneous file utility methods.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanReadAndWriteFile(@Nullable File aFile) Check if the passed file can read and write.static @NonNull com.helger.base.state.EChangeensureParentDirectoryIsPresent(@NonNull File aFile) Ensure that the parent directory of the passed file is present.static booleanequalFiles(@NonNull File aObj1, @NonNull File aObj2) Compare two files by their clean absolute paths for equality.static booleanCheck if the passed directory exists.static booleanexistsFile(@Nullable File aFile) Check if the passed file exists.static @Nullable URLConvert the passed file to aURL.static @Nullable StringgetAsURLString(@NonNull File aFile) Convert the passed file to a URL string representation.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedInputStreamgetBufferedInputStream(@NonNull File aFile) Get a buffered input stream for the passed file.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStreamgetBufferedOutputStream(@NonNull File aFile) Get a buffered output stream for the passed file using the default append mode.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStreamgetBufferedOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend) Get a buffered output stream for the passed file.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedReadergetBufferedReader(@NonNull File aFile, @NonNull Charset aCharset) Get a bufferedReaderfor the passed file using the specified charset.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWritergetBufferedWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get a bufferedWriterfor the passed file using the specified append mode and charset.static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWritergetBufferedWriter(@NonNull File aFile, @NonNull Charset aCharset) Get a bufferedWriterfor the passed file using the default append mode and the specified charset.static @Nullable FilegetCanonicalFile(@Nullable File aFile) Get the canonical file of the passed file, if the file is notnull.static @Nullable FilegetCanonicalFileOrNull(@Nullable File aFile) Get the canonical file of the passed file, if the file is notnull.static @Nullable StringgetCanonicalPath(@Nullable File aFile) Get the canonical path of the passed file, if the file is notnull.static @Nullable StringgetCanonicalPathOrNull(@Nullable File aFile) Get the canonical path of the passed file, if the file is notnull.static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory) This is a replacement forFile.listFiles()doing some additional checks on permissions.static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory, @NonNull FileFilter aFileFilter) This is a replacement forFile.listFiles(FileFilter)doing some additional checks on permissions.static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory, @NonNull FilenameFilter aFilenameFilter) This is a replacement forFile.listFiles(FilenameFilter)doing some additional checks on permissions.static intgetDirectoryObjectCount(@NonNull File aDirectory) Returns the number of files and directories contained in the passed directory excluding the system internal directories.static @NonNull StringgetFileSizeDisplay(long nFileSize) Get a human-readable file size display string for the passed file size using no decimal places.static @NonNull StringgetFileSizeDisplay(long nFileSize, int nDecimals) Get a human-readable file size display string for the passed file size.static @NonNull StringgetFileSizeDisplay(@Nullable File aFile) Get a human-readable file size display string for the passed file using no decimal places.static @NonNull StringgetFileSizeDisplay(@Nullable File aFile, int nDecimals) Get a human-readable file size display string for the passed file.static @Nullable FileInputStreamgetInputStream(@NonNull File aFile) Get aFileInputStreamfor the passed file.static @Nullable FileOutputStreamgetOutputStream(@NonNull File aFile) Get an output stream for writing to a file.static @Nullable FileOutputStreamgetOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend) Get an output stream for writing to a file.static @Nullable PrintWritergetPrintWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aPrintWriterfor the passed file using the specified append mode and charset.static @Nullable PrintWritergetPrintWriter(@NonNull File aFile, @NonNull Charset aCharset) Get aPrintWriterfor the passed file using the default append mode and the specified charset.static @Nullable RandomAccessFilegetRandomAccessFile(@NonNull File aFile, @NonNull ERandomAccessFileMode eMode) Get aRandomAccessFilefor the passed file and mode.static @Nullable RandomAccessFilegetRandomAccessFile(@NonNull String sFilename, @NonNull ERandomAccessFileMode eMode) Get aRandomAccessFilefor the passed filename and mode.static @Nullable InputStreamReaderGet aReaderfor the passed file using the specified charset.static @Nullable FilegetSecureFile(@Nullable File aFile) Get a secureFileobject based on the passed file object.static @Nullable OutputStreamWritergetWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aWriterfor the passed file using the specified append mode and charset.static @Nullable OutputStreamWriterGet aWriterfor the passed file using the default append mode and the specified charset.static booleanisFileNewer(@NonNull File aFile1, @NonNull File aFile2) Returnstrueif the first file is newer than the second file.static booleanisParentDirectory(@NonNull File aSearchDirectory, @NonNull File aStartDirectory) Check if the searched directory is a parent object of the start directorystatic booleanCheck if warnings on file operation issues are enabled.static voidsetWarnOnIssues(boolean bWarnOnIssues) Enable or disable warnings on file operation issues.
-
Method Details
-
isWarnOnIssues
public static boolean isWarnOnIssues()Check if warnings on file operation issues are enabled.- Returns:
trueif warnings are enabled,falseotherwise.
-
setWarnOnIssues
public static void setWarnOnIssues(boolean bWarnOnIssues) Enable or disable warnings on file operation issues.- Parameters:
bWarnOnIssues-trueto enable warnings,falseto disable them.
-
equalFiles
Compare two files by their clean absolute paths for equality.- Parameters:
aObj1- The first file. May not benull.aObj2- The second file. May not benull.- Returns:
trueif the two files point to the same path,falseotherwise.
-
existsFile
Check if the passed file exists. Must be existing and a file.- Parameters:
aFile- The file to be checked for existence. May benull.- Returns:
trueif the passed file is non-null, is a file and exists,falseotherwise.
-
existsDir
Check if the passed directory exists. Must be existing and must be a directory!- Parameters:
aDir- The directory to be checked for existence. May benull.- Returns:
trueif the passed directory is notnull, is a directory and exists,falseotherwise.
-
canReadAndWriteFile
Check if the passed file can read and write. If the file already exists, the file itself is checked. If the file does not exist, the parent directory- Parameters:
aFile- The file to be checked. May benull.- Returns:
trueif the file can be read and written
-
ensureParentDirectoryIsPresent
public static @NonNull com.helger.base.state.EChange ensureParentDirectoryIsPresent(@NonNull File aFile) Ensure that the parent directory of the passed file is present. If it is not present, it will be created.- Parameters:
aFile- The file whose parent directory should be present. May not benull.- Returns:
EChange.CHANGEDif the parent directory was created,EChange.UNCHANGEDif it already existed.- Throws:
IllegalStateException- If the parent directory could not be created.
-
getCanonicalFile
Get the canonical file of the passed file, if the file is notnull.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.- Throws:
IOException- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
-
getCanonicalFileOrNull
Get the canonical file of the passed file, if the file is notnull. In case of anIOException,nullis returned.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnullor an exception occurred.
-
getCanonicalPath
Get the canonical path of the passed file, if the file is notnull.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.- Throws:
IOException- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
-
getCanonicalPathOrNull
Get the canonical path of the passed file, if the file is notnull. In case of anIOException,nullis returned.- Parameters:
aFile- The file to get the canonical path from. May benull.- Returns:
nullif the passed file isnull.
-
isParentDirectory
public static boolean isParentDirectory(@NonNull File aSearchDirectory, @NonNull File aStartDirectory) Check if the searched directory is a parent object of the start directory- Parameters:
aSearchDirectory- The directory to be searched. May not benull.aStartDirectory- The directory where the search starts. May not benull.- Returns:
trueif the search directory is a parent of the start directory,falseotherwise.- See Also:
-
getInputStream
Get aFileInputStreamfor the passed file.- Parameters:
aFile- The file to open for reading. May not benull.- Returns:
nullif the file does not exist or cannot be opened.
-
getBufferedInputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedInputStream getBufferedInputStream(@NonNull File aFile) Get a buffered input stream for the passed file.- Parameters:
aFile- The file to open for reading. May not benull.- Returns:
nullif the file does not exist or cannot be opened.
-
getReader
Get aReaderfor the passed file using the specified charset.- Parameters:
aFile- The file to open for reading. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file does not exist or cannot be opened.
-
getBufferedReader
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedReader getBufferedReader(@NonNull File aFile, @NonNull Charset aCharset) Get a bufferedReaderfor the passed file using the specified charset.- Parameters:
aFile- The file to open for reading. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file does not exist or cannot be opened.
-
getOutputStream
Get an output stream for writing to a file.- Parameters:
aFile- The file to write to. May not benull.- Returns:
nullif the file could not be opened
-
getOutputStream
public static @Nullable FileOutputStream getOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend) Get an output stream for writing to a file.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.- Returns:
nullif the file could not be opened
-
getBufferedOutputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull File aFile) Get a buffered output stream for the passed file using the default append mode.- Parameters:
aFile- The file to write to. May not benull.- Returns:
nullif the file could not be opened.
-
getBufferedOutputStream
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedOutputStream getBufferedOutputStream(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend) Get a buffered output stream for the passed file.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.- Returns:
nullif the file could not be opened.
-
getWriter
public static @Nullable OutputStreamWriter getWriter(@NonNull File aFile, @NonNull Charset aCharset) Get aWriterfor the passed file using the default append mode and the specified charset.- Parameters:
aFile- The file to write to. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getWriter
public static @Nullable OutputStreamWriter getWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aWriterfor the passed file using the specified append mode and charset.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getBufferedWriter
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWriter getBufferedWriter(@NonNull File aFile, @NonNull Charset aCharset) Get a bufferedWriterfor the passed file using the default append mode and the specified charset.- Parameters:
aFile- The file to write to. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getBufferedWriter
public static @Nullable com.helger.base.io.nonblocking.NonBlockingBufferedWriter getBufferedWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get a bufferedWriterfor the passed file using the specified append mode and charset.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getPrintWriter
Get aPrintWriterfor the passed file using the default append mode and the specified charset.- Parameters:
aFile- The file to write to. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getPrintWriter
public static @Nullable PrintWriter getPrintWriter(@NonNull File aFile, @NonNull com.helger.base.io.EAppend eAppend, @NonNull Charset aCharset) Get aPrintWriterfor the passed file using the specified append mode and charset.- Parameters:
aFile- The file to write to. May not benull.eAppend- Appending mode. May not benull.aCharset- The charset to use. May not benull.- Returns:
nullif the file could not be opened.
-
getRandomAccessFile
public static @Nullable RandomAccessFile getRandomAccessFile(@NonNull String sFilename, @NonNull ERandomAccessFileMode eMode) Get aRandomAccessFilefor the passed filename and mode.- Parameters:
sFilename- The name of the file to open. May not benull.eMode- The access mode. May not benull.- Returns:
nullif the file could not be opened.
-
getRandomAccessFile
public static @Nullable RandomAccessFile getRandomAccessFile(@NonNull File aFile, @NonNull ERandomAccessFileMode eMode) Get aRandomAccessFilefor the passed file and mode.- Parameters:
aFile- The file to open. May not benull.eMode- The access mode. May not benull.- Returns:
nullif the file could not be opened.
-
isFileNewer
Returnstrueif the first file is newer than the second file. Returnstrueif the first file exists and the second file does not exist. Returnsfalseif the first file is older than the second file. Returnsfalseif the first file does not exists but the second does. Returnsfalseif none of the files exist.- Parameters:
aFile1- First file. May not benull.aFile2- Second file. May not benull.- Returns:
trueif the first file is newer than the second file,falseotherwise.
-
getFileSizeDisplay
Get a human-readable file size display string for the passed file using no decimal places.- Parameters:
aFile- The file to get the size display for. May benull.- Returns:
- An empty string if the file is
nullor does not exist; otherwise a human-readable size string.
-
getFileSizeDisplay
Get a human-readable file size display string for the passed file.- Parameters:
aFile- The file to get the size display for. May benull.nDecimals- The number of decimal places to use. Must be ≥ 0.- Returns:
- An empty string if the file is
nullor does not exist; otherwise a human-readable size string.
-
getFileSizeDisplay
Get a human-readable file size display string for the passed file size using no decimal places.- Parameters:
nFileSize- The file size in bytes. Must be ≥ 0.- Returns:
- A human-readable size string. Never
null.
-
getFileSizeDisplay
public static @NonNull String getFileSizeDisplay(@Nonnegative long nFileSize, @Nonnegative int nDecimals) Get a human-readable file size display string for the passed file size.- Parameters:
nFileSize- The file size in bytes. Must be ≥ 0.nDecimals- The number of decimal places to use. Must be ≥ 0.- Returns:
- A human-readable size string. Never
null.
-
getSecureFile
Get a secureFileobject based on the passed file object. First all relative paths ("." and "..") are resolved and all eventually contained '\0' characters are eliminated. Than all file names are checked for validity (so that no special characters are contained).- Parameters:
aFile- The file to be secured.- Returns:
nullif the passed file isnull.
-
getDirectoryObjectCount
Returns the number of files and directories contained in the passed directory excluding the system internal directories.- Parameters:
aDirectory- The directory to check. May not benulland must be a directory.- Returns:
- A non-negative number of objects in that directory.
- See Also:
-
getDirectoryContent
@ReturnsMutableCopy public static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory) This is a replacement forFile.listFiles()doing some additional checks on permissions. The order of the returned files is defined by the underlyingFile.listFiles()method.- Parameters:
aDirectory- The directory to be listed. May not benull.- Returns:
- Never
null.
-
getDirectoryContent
@ReturnsMutableCopy public static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory, @NonNull FilenameFilter aFilenameFilter) This is a replacement forFile.listFiles(FilenameFilter)doing some additional checks on permissions. The order of the returned files is defined by the underlyingFile.listFiles(FilenameFilter)method.- Parameters:
aDirectory- The directory to be listed. May not benull.aFilenameFilter- The filename filter to be used. May not benull.- Returns:
- Never
null.
-
getDirectoryContent
@ReturnsMutableCopy public static @NonNull com.helger.collection.commons.ICommonsList<File> getDirectoryContent(@NonNull File aDirectory, @NonNull FileFilter aFileFilter) This is a replacement forFile.listFiles(FileFilter)doing some additional checks on permissions. The order of the returned files is defined by the underlyingFile.listFiles(FileFilter)method.- Parameters:
aDirectory- The directory to be listed. May not benull.aFileFilter- The file filter to be used. May not benull.- Returns:
- Never
null.
-
getAsURL
Convert the passed file to aURL.- Parameters:
aFile- The file to be converted. May not benull.- Returns:
nullif the conversion to URL failed.
-
getAsURLString
Convert the passed file to a URL string representation.- Parameters:
aFile- The file to be converted. May not benull.- Returns:
nullif the conversion to URL failed.
-