Package com.helger.commons.io.file
Class FileHelper
- java.lang.Object
-
- com.helger.commons.io.file.FileHelper
-
@Immutable public final class FileHelper extends Object
Miscellaneous file utility methods.- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanReadAndWriteFile(File aFile)Check if the passed file can read and write.static EChangeensureParentDirectoryIsPresent(File aFile)static booleanexistsDir(File aDir)Check if the passed directory exists.static booleanexistsFile(File aFile)Check if the passed file exists.static URLgetAsURL(File aFile)static StringgetAsURLString(File aFile)static NonBlockingBufferedInputStreamgetBufferedInputStream(File aFile)static NonBlockingBufferedOutputStreamgetBufferedOutputStream(File aFile)static NonBlockingBufferedOutputStreamgetBufferedOutputStream(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 InputStreamReadergetReader(File aFile, Charset aCharset)static FilegetSecureFile(File aFile)Get a secureFileobject based on the passed file object.static OutputStreamWritergetWriter(File aFile, EAppend eAppend, Charset aCharset)static OutputStreamWritergetWriter(File aFile, Charset aCharset)static 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 Detail
-
existsFile
public static boolean existsFile(@Nullable File aFile)
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
public static boolean existsDir(@Nullable File aDir)
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
public static boolean canReadAndWriteFile(@Nullable File aFile)
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
@Nonnull public static EChange ensureParentDirectoryIsPresent(@Nonnull File aFile)
-
getCanonicalFile
@Nullable public static File getCanonicalFile(@Nullable File aFile) throws IOException
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
@Nullable public static File getCanonicalFileOrNull(@Nullable File aFile)
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
@Nullable public static String getCanonicalPath(@Nullable File aFile) throws IOException
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
@Nullable public static String getCanonicalPathOrNull(@Nullable File aFile)
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:
getCanonicalFile(File)
-
getInputStream
@Nullable public static FileInputStream getInputStream(@Nonnull File aFile)
-
getBufferedInputStream
@Nullable public static NonBlockingBufferedInputStream getBufferedInputStream(@Nonnull File aFile)
-
getReader
@Nullable public static InputStreamReader getReader(@Nonnull File aFile, @Nonnull Charset aCharset)
-
getBufferedReader
@Nullable public static NonBlockingBufferedReader getBufferedReader(@Nonnull File aFile, @Nonnull Charset aCharset)
-
getOutputStream
@Nullable public static FileOutputStream getOutputStream(@Nonnull File aFile)
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
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull File aFile, @Nonnull EAppend eAppend)
-
getWriter
@Nullable public static OutputStreamWriter getWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
-
getWriter
@Nullable public static OutputStreamWriter getWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
-
getBufferedWriter
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
-
getBufferedWriter
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
-
getPrintWriter
@Nullable public static PrintWriter getPrintWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
-
getPrintWriter
@Nullable public static PrintWriter getPrintWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
-
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
public static boolean isFileNewer(@Nonnull File aFile1, @Nonnull File aFile2)
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
@Nonnull public static String getFileSizeDisplay(@Nullable File aFile, @Nonnegative int nDecimals)
-
getFileSizeDisplay
@Nonnull public static String getFileSizeDisplay(@Nonnegative long nFileSize, @Nonnegative int nDecimals)
-
getSecureFile
@Nullable public static File getSecureFile(@Nullable File aFile)
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
@Nonnegative public static int getDirectoryObjectCount(@Nonnull File aDirectory)
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:
FilenameHelper.isSystemInternalDirectory(CharSequence)
-
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.
-
-