@Immutable public final class FileHelper extends Object
| Modifier and Type | Method and Description |
|---|---|
static boolean |
canExecute(File aFile)
Tests whether the application can execute the file denoted by this abstract
pathname.
|
static boolean |
canRead(File aFile)
Tests whether the application can read the file denoted by this abstract
pathname.
|
static boolean |
canReadAndWriteFile(File aFile)
Check if the passed file can read and write.
|
static boolean |
canWrite(File aFile)
Tests whether the application can modify the file denoted by this abstract
pathname.
|
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 Reader |
getBufferedReader(File aFile,
Charset aCharset) |
static Reader |
getBufferedReader(String sFilename,
Charset aCharset) |
static Writer |
getBufferedWriter(File aFile,
EAppend eAppend,
Charset aCharset) |
static Writer |
getBufferedWriter(String sFilename,
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 List<File> |
getDirectoryContent(File aDirectory)
This is a replacement for
File.listFiles() doing some
additional checks on permissions. |
static List<File> |
getDirectoryContent(File aDirectory,
FileFilter aFileFilter)
This is a replacement for
File.listFiles(FileFilter) doing
some additional checks on permissions. |
static List<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 FileChannel |
getFileReadChannel(File aFile) |
static FileChannel |
getFileReadChannel(String sFilename) |
static long |
getFileSize(FileChannel aChannel) |
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 FileChannel |
getFileWriteChannel(File aFile) |
static FileChannel |
getFileWriteChannel(File aFile,
EAppend eAppend) |
static FileChannel |
getFileWriteChannel(String sFilename) |
static FileChannel |
getFileWriteChannel(String sFilename,
EAppend eAppend) |
static InputStream |
getInputStream(File aFile) |
static InputStream |
getInputStream(String sFilename) |
static InputStream |
getMappedInputStream(File aFile)
Get an input stream to the specified file, using memory mapping.
|
static OutputStream |
getMappedOutputStream(File aFile) |
static OutputStream |
getMappedOutputStream(File aFile,
EAppend eAppend) |
static OutputStream |
getMappedOutputStream(String sFilename) |
static OutputStream |
getMappedOutputStream(String sFilename,
EAppend eAppend) |
static OutputStream |
getOutputStream(File aFile)
Get an output stream for writing to a file.
|
static OutputStream |
getOutputStream(File aFile,
EAppend eAppend)
Get an output stream for writing to a file.
|
static OutputStream |
getOutputStream(String sFilename)
Get an output stream for writing to a file.
|
static OutputStream |
getOutputStream(String sFilename,
EAppend eAppend)
Get an output stream for writing to a file.
|
static RandomAccessFile |
getRandomAccessFile(File aFile,
ERandomAccessFileMode eMode) |
static RandomAccessFile |
getRandomAccessFile(String sFilename,
ERandomAccessFileMode eMode) |
static Reader |
getReader(File aFile,
Charset aCharset) |
static Reader |
getReader(String sFilename,
Charset aCharset) |
static File |
getSecureFile(File aFile)
Get a secure
File object based on the passed file object. |
static Writer |
getWriter(File aFile,
EAppend eAppend,
Charset aCharset) |
static Writer |
getWriter(String sFilename,
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 canRead(@Nullable File aFile)
aFile - The file to be checked. May be null.true if and only if the file specified by this
abstract pathname is not null, exists and can
be read by the application; false otherwisepublic static boolean canWrite(@Nullable File aFile)
aFile - The file to be checked. May be null.true if and only if the parameter is not
null, the file system actually contains a file denoted
by this abstract pathname and the application is allowed
to write to the file; false otherwise.public static boolean canExecute(@Nullable File aFile)
aFile - The file to be checked. May be null.true if and only if the abstract pathname is not
null, exists and the application is allowed
to execute the filepublic 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 FileChannel getFileReadChannel(@Nonnull String sFilename)
@Nullable public static FileChannel getFileReadChannel(@Nonnull File aFile)
@CheckForSigned public static long getFileSize(@Nonnull @WillNotClose FileChannel aChannel)
@Nullable public static InputStream getInputStream(@Nonnull String sFilename)
@Nullable public static InputStream getInputStream(@Nonnull File aFile)
@Nullable public static Reader getReader(@Nonnull String sFilename, @Nonnull Charset aCharset)
@Nullable public static Reader getBufferedReader(@Nonnull String sFilename, @Nonnull Charset aCharset)
@Nullable public static Reader getBufferedReader(@Nonnull File aFile, @Nonnull Charset aCharset)
@Nullable public static InputStream getMappedInputStream(@Nonnull File aFile)
FileInputStream is returned.aFile - The file to use. May not be null.@Nullable public static FileChannel getFileWriteChannel(@Nonnull String sFilename)
@Nullable public static FileChannel getFileWriteChannel(@Nonnull String sFilename, @Nonnull EAppend eAppend)
@Nullable public static FileChannel getFileWriteChannel(@Nonnull File aFile)
@Nullable public static FileChannel getFileWriteChannel(@Nonnull File aFile, @Nonnull EAppend eAppend)
@Nullable public static OutputStream getOutputStream(@Nonnull String sFilename)
sFilename - The name of the file to write to. May not be null.null if the file could not be opened@Nullable public static OutputStream getOutputStream(@Nonnull String sFilename, @Nonnull EAppend eAppend)
sFilename - The name of 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 OutputStream 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 Writer getWriter(@Nonnull String sFilename, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static Writer getWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static Writer getBufferedWriter(@Nonnull String sFilename, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static Writer getBufferedWriter(@Nonnull File aFile, @Nonnull EAppend eAppend, @Nonnull Charset aCharset)
@Nullable public static OutputStream 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 OutputStream getMappedOutputStream(@Nonnull String sFilename)
@Nullable public static OutputStream getMappedOutputStream(@Nonnull String sFilename, @Nonnull EAppend eAppend)
@Nullable public static OutputStream getMappedOutputStream(@Nonnull File aFile)
@Nullable public static OutputStream getMappedOutputStream(@Nonnull File aFile, @Nonnull EAppend eAppend)
@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 List<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 List<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 List<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–2015 Philip Helger. All rights reserved.