Package com.helger.commons.io.file
Class FileHelper
java.lang.Object
com.helger.commons.io.file.FileHelper
Miscellaneous file utility methods.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanReadAndWriteFile(File aFile) Check if the passed file can read and write.static EChangestatic booleanCheck if the passed directory exists.static booleanexistsFile(File aFile) Check if the passed file exists.static URLstatic StringgetAsURLString(File aFile) getBufferedInputStream(File aFile) getBufferedOutputStream(File aFile) getBufferedOutputStream(File aFile, EAppend eAppend) static NonBlockingBufferedReadergetBufferedReader(File aFile, Charset aCharset) static NonBlockingBufferedWritergetBufferedWriter(File aFile, EAppend eAppend, Charset aCharset) static NonBlockingBufferedWritergetBufferedWriter(File aFile, Charset aCharset) static FilegetCanonicalFile(File aFile) Get the canonical file of the passed file, if the file is notnull.static FilegetCanonicalFileOrNull(File aFile) Get the canonical file of the passed file, if the file is notnull.static StringgetCanonicalPath(File aFile) Get the canonical path of the passed file, if the file is notnull.static StringgetCanonicalPathOrNull(File aFile) Get the canonical path of the passed file, if the file is notnull.static ICommonsList<File> getDirectoryContent(File aDirectory) This is a replacement forFile.listFiles()doing some additional checks on permissions.static ICommonsList<File> getDirectoryContent(File aDirectory, FileFilter aFileFilter) This is a replacement forFile.listFiles(FileFilter)doing some additional checks on permissions.static ICommonsList<File> getDirectoryContent(File aDirectory, FilenameFilter aFilenameFilter) This is a replacement forFile.listFiles(FilenameFilter)doing some additional checks on permissions.static intgetDirectoryObjectCount(File aDirectory) Returns the number of files and directories contained in the passed directory excluding the system internal directories.static StringgetFileSizeDisplay(long nFileSize) static StringgetFileSizeDisplay(long nFileSize, int nDecimals) static StringgetFileSizeDisplay(File aFile) static StringgetFileSizeDisplay(File aFile, int nDecimals) static FileInputStreamgetInputStream(File aFile) static FileOutputStreamgetOutputStream(File aFile) Get an output stream for writing to a file.static FileOutputStreamgetOutputStream(File aFile, EAppend eAppend) Get an output stream for writing to a file.static PrintWritergetPrintWriter(File aFile, EAppend eAppend, Charset aCharset) static PrintWritergetPrintWriter(File aFile, Charset aCharset) static RandomAccessFilegetRandomAccessFile(File aFile, ERandomAccessFileMode eMode) static RandomAccessFilegetRandomAccessFile(String sFilename, ERandomAccessFileMode eMode) static InputStreamReaderstatic FilegetSecureFile(File aFile) Get a secureFileobject based on the passed file object.static OutputStreamWriterstatic OutputStreamWriterstatic booleanisFileNewer(File aFile1, File aFile2) Returnstrueif the first file is newer than the second file.static booleanisParentDirectory(File aSearchDirectory, File aStartDirectory) Check if the searched directory is a parent object of the start directory
-
Method Details
-
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
-
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
-
getBufferedInputStream
-
getReader
-
getBufferedReader
-
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
@Nullable public static FileOutputStream getOutputStream(@Nonnull File aFile, @Nonnull 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
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull File aFile) -
getBufferedOutputStream
-
getWriter
-
getWriter
-
getBufferedWriter
-
getBufferedWriter
-
getPrintWriter
-
getPrintWriter
-
getRandomAccessFile
@Nullable public static RandomAccessFile getRandomAccessFile(@Nonnull String sFilename, @Nonnull ERandomAccessFileMode eMode) -
getRandomAccessFile
@Nullable public static RandomAccessFile getRandomAccessFile(@Nonnull File aFile, @Nonnull ERandomAccessFileMode eMode) -
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
-
getFileSizeDisplay
-
getFileSizeDisplay
-
getFileSizeDisplay
@Nonnull public static String getFileSizeDisplay(@Nonnegative long nFileSize, @Nonnegative int nDecimals) -
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
@Nonnull @ReturnsMutableCopy public static 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
@Nonnull @ReturnsMutableCopy public static 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
@Nonnull @ReturnsMutableCopy public static 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
-
getAsURLString
-