@Immutable public final class FileHelper extends Object
| Modifier and Type | Method and Description |
|---|---|
static boolean |
canReadAndWriteFile(File aFile)
Check if the passed file can read and write.
|
static EChange |
ensureParentDirectoryIsPresent(File aFile) |
static boolean |
existsDir(File aDir)
Check if the passed directory exists.
|
static boolean |
existsFile(File aFile)
Check if the passed file exists.
|
static URL |
getAsURL(File aFile) |
static String |
getAsURLString(File aFile) |
static NonBlockingBufferedInputStream |
getBufferedInputStream(File aFile) |
static NonBlockingBufferedOutputStream |
getBufferedOutputStream(File aFile) |
static NonBlockingBufferedOutputStream |
getBufferedOutputStream(File aFile,
EAppend eAppend) |
static NonBlockingBufferedReader |
getBufferedReader(File aFile,
Charset aCharset) |
static NonBlockingBufferedWriter |
getBufferedWriter(File aFile,
Charset aCharset) |
static NonBlockingBufferedWriter |
getBufferedWriter(File aFile,
EAppend eAppend,
Charset aCharset) |
static File |
getCanonicalFile(File aFile)
Get the canonical file of the passed file, if the file is not
null. |
static File |
getCanonicalFileOrNull(File aFile)
Get the canonical file of the passed file, if the file is not
null. |
static String |
getCanonicalPath(File aFile)
Get the canonical path of the passed file, if the file is not
null. |
static String |
getCanonicalPathOrNull(File aFile)
Get the canonical path of the passed file, if the file is not
null. |
static ICommonsList<File> |
getDirectoryContent(File aDirectory)
This is a replacement for
File.listFiles() doing some
additional checks on permissions. |
static ICommonsList<File> |
getDirectoryContent(File aDirectory,
FileFilter aFileFilter)
This is a replacement for
File.listFiles(FileFilter) doing
some additional checks on permissions. |
static ICommonsList<File> |
getDirectoryContent(File aDirectory,
FilenameFilter aFilenameFilter)
This is a replacement for
File.listFiles(FilenameFilter) doing
some additional checks on permissions. |
static int |
getDirectoryObjectCount(File aDirectory)
Returns the number of files and directories contained in the passed
directory excluding the system internal directories.
|
static String |
getFileSizeDisplay(File aFile) |
static String |
getFileSizeDisplay(File aFile,
int nDecimals) |
static String |
getFileSizeDisplay(long nFileSize) |
static String |
getFileSizeDisplay(long nFileSize,
int nDecimals) |
static FileInputStream |
getInputStream(File aFile) |
static FileOutputStream |
getOutputStream(File aFile)
Get an output stream for writing to a file.
|
static FileOutputStream |
getOutputStream(File aFile,
EAppend eAppend)
Get an output stream for writing to a file.
|
static PrintWriter |
getPrintWriter(File aFile,
Charset aCharset) |
static PrintWriter |
getPrintWriter(File aFile,
EAppend eAppend,
Charset aCharset) |
static RandomAccessFile |
getRandomAccessFile(File aFile,
ERandomAccessFileMode eMode) |
static RandomAccessFile |
getRandomAccessFile(String sFilename,
ERandomAccessFileMode eMode) |
static InputStreamReader |
getReader(File aFile,
Charset aCharset) |
static File |
getSecureFile(File aFile)
Get a secure
File object based on the passed file object. |
static OutputStreamWriter |
getWriter(File aFile,
Charset aCharset) |
static OutputStreamWriter |
getWriter(File aFile,
EAppend eAppend,
Charset aCharset) |
static boolean |
isFileNewer(File aFile1,
File aFile2)
Returns
true if the first file is newer than the second file. |
static boolean |
isParentDirectory(File aSearchDirectory,
File aStartDirectory)
Check if the searched directory is a parent object of the start directory
|
public static boolean existsFile(@Nullable File aFile)
aFile - The file to be checked for existence. May be null .true if the passed file is non-null, is a
file and exists, false otherwise.public static boolean existsDir(@Nullable File aDir)
aDir - The directory to be checked for existence. May be null.true if the passed directory is not null,
is a directory and exists, false otherwise.public static boolean canReadAndWriteFile(@Nullable File aFile)
aFile - The file to be checked. May be null.true if the file can be read and written@Nonnull public static EChange ensureParentDirectoryIsPresent(@Nonnull File aFile)
@Nullable public static File getCanonicalFile(@Nullable File aFile) throws IOException
null.aFile - The file to get the canonical path from. May be null.null if the passed file is null.IOException - If an I/O error occurs, which is possible because the construction
of the canonical pathname may require filesystem queries@Nullable public static File getCanonicalFileOrNull(@Nullable File aFile)
null. In case of an IOException, null is
returned.aFile - The file to get the canonical path from. May be null.null if the passed file is null or an
exception occurred.@Nullable public static String getCanonicalPath(@Nullable File aFile) throws IOException
null.aFile - The file to get the canonical path from. May be null.null if the passed file is null.IOException - If an I/O error occurs, which is possible because the construction
of the canonical pathname may require filesystem queries@Nullable public static String getCanonicalPathOrNull(@Nullable File aFile)
null. In case of an IOException, null is
returned.aFile - The file to get the canonical path from. May be null.null if the passed file is null.public static boolean isParentDirectory(@Nonnull File aSearchDirectory, @Nonnull File aStartDirectory)
aSearchDirectory - The directory to be searched. May not be null.aStartDirectory - The directory where the search starts. May not be null.true if the search directory is a parent of the start
directory, false otherwise.getCanonicalFile(File)@Nullable public static FileInputStream getInputStream(@Nonnull File aFile)
@Nullable public static NonBlockingBufferedInputStream getBufferedInputStream(@Nonnull File aFile)
@Nullable public static InputStreamReader getReader(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static NonBlockingBufferedReader getBufferedReader(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static FileOutputStream getOutputStream(@Nonnull File aFile)
aFile - The file to write to. May not be null.null if the file could not be opened@Nullable public static FileOutputStream getOutputStream(@Nonnull File aFile, @Nonnull EAppend eAppend)
aFile - The file to write to. May not be null.eAppend - Appending mode. May not be null.null if the file could not be opened@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull File aFile)
@Nullable public static NonBlockingBufferedOutputStream getBufferedOutputStream(@Nonnull File aFile, @Nonnull EAppend eAppend)
@Nullable public static OutputStreamWriter getWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static OutputStreamWriter getWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static NonBlockingBufferedWriter getBufferedWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static PrintWriter getPrintWriter(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static PrintWriter getPrintWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static RandomAccessFile getRandomAccessFile(@Nonnull String sFilename, @Nonnull ERandomAccessFileMode eMode)
@Nullable public static RandomAccessFile getRandomAccessFile(@Nonnull File aFile, @Nonnull ERandomAccessFileMode eMode)
public static boolean isFileNewer(@Nonnull File aFile1, @Nonnull File aFile2)
true if the first file is newer than the second file.
Returns true if the first file exists and the second file does
not exist. Returns false if the first file is older than the
second file. Returns false if the first file does not exists
but the second does. Returns false if none of the files exist.aFile1 - First file. May not be null.aFile2 - Second file. May not be null.true if the first file is newer than the second file,
false otherwise.@Nonnull public static String getFileSizeDisplay(@Nullable File aFile, @Nonnegative int nDecimals)
@Nonnull public static String getFileSizeDisplay(@Nonnegative long nFileSize, @Nonnegative int nDecimals)
@Nullable public static File getSecureFile(@Nullable File aFile)
File object 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).aFile - The file to be secured.null if the passed file is null.@Nonnegative public static int getDirectoryObjectCount(@Nonnull File aDirectory)
aDirectory - The directory to check. May not be null and must be a
directory.FilenameHelper.isSystemInternalDirectory(CharSequence)@Nonnull @ReturnsMutableCopy public static ICommonsList<File> getDirectoryContent(@Nonnull File aDirectory)
File.listFiles() doing some
additional checks on permissions. The order of the returned files is
defined by the underlying File.listFiles() method.aDirectory - The directory to be listed. May not be null.null.@Nonnull @ReturnsMutableCopy public static ICommonsList<File> getDirectoryContent(@Nonnull File aDirectory, @Nonnull FilenameFilter aFilenameFilter)
File.listFiles(FilenameFilter) doing
some additional checks on permissions. The order of the returned files is
defined by the underlying File.listFiles(FilenameFilter) method.aDirectory - The directory to be listed. May not be null.aFilenameFilter - The filename filter to be used. May not be null.null.@Nonnull @ReturnsMutableCopy public static ICommonsList<File> getDirectoryContent(@Nonnull File aDirectory, @Nonnull FileFilter aFileFilter)
File.listFiles(FileFilter) doing
some additional checks on permissions. The order of the returned files is
defined by the underlying File.listFiles(FileFilter) method.aDirectory - The directory to be listed. May not be null.aFileFilter - The file filter to be used. May not be null.null.Copyright © 2014–2022 Philip Helger. All rights reserved.