public final class FileUtil extends Object
Modifier and Type | Method and Description |
---|---|
static File |
createDateDirs(File parentDir)
Creates (if not already existing) a series of directories reflecting
the current date, up to the day unit, under a given parent directory.
|
static File |
createDateDirs(File parentDir,
Date date)
Creates (if not already existing) a series of directories reflecting
a date, up to the day unit, under a given parent directory.
|
static File |
createDateFormattedDirs(File parentDir,
Date dateTime,
String format)
Creates (if not already existing) a series of directories reflecting
the specified date format (from
SimpleDateFormat ),
under a given parent directory. |
static File |
createDateTimeDirs(File parentDir)
Creates (if not already existing) a series of directories reflecting
the current date and time, up to the seconds, under a given parent
directory.
|
static File |
createDateTimeDirs(File parentDir,
Date dateTime)
Creates (if not already existing) a series of directories reflecting
a date and time, up to the seconds, under a given parent directory.
|
static File |
createDirsForFile(File file)
Create all parent directories for a file if they do not exists.
|
static File |
createURLDirs(File parentDir,
String url)
Creates (if not already existing) a series of directories
matching URL segments, under a given parent directory.
|
static File |
createURLDirs(File parentDir,
String url,
boolean truncate)
Creates (if not already existing) a series of directories
matching URL segments, under a given parent directory.
|
static File |
createURLDirs(File parentDir,
URL url)
Creates (if not already existing) a series of directories
matching URL segments, under a given parent directory.
|
static File |
createURLDirs(File parentDir,
URL url,
boolean truncate)
Creates (if not already existing) a series of directories
matching URL segments, under a given parent directory.
|
static void |
delete(File file)
Deletes a file or a directory recursively in a more robust way.
|
static int |
deleteEmptyDirs(File parentDir)
Deletes all directories that are empty from a given parent directory.
|
static int |
deleteEmptyDirs(File parentDir,
Date date)
Deletes all directories that are empty and are older
than the given date.
|
static boolean |
dirEmpty(File dir)
Gets whether a directory is empty of files or directories
in an efficient way which does not load all files.
|
static boolean |
dirHasFile(File dir,
FileFilter filter)
Recursively gets whether a directory contains at least one file
matching the given file filter.
|
static String |
fromSafeFileName(String safeFileName)
Converts a "safe" file name originally created with
toSafeFileName(String) into its original string. |
static String[] |
head(File file,
int numberOfLinesToRead)
Returns the specified number of lines starting from the beginning
of a text file.
|
static String[] |
head(File file,
String encoding,
int numberOfLinesToRead)
Returns the specified number of lines starting from the beginning
of a text file, using the given encoding.
|
static String[] |
head(File file,
String encoding,
int numberOfLinesToRead,
boolean stripBlankLines)
Returns the specified number of lines starting from the beginning
of a text file, using the given encoding.
|
static String[] |
head(File file,
String encoding,
int numberOfLinesToRead,
boolean stripBlankLines,
IInputStreamFilter filter)
Returns the specified number of lines starting from the beginning
of a text file, using the given encoding.
|
static void |
moveFile(File sourceFile,
File targetFile)
Moves a file to a new file location.
|
static void |
moveFileToDir(File sourceFile,
File targetDir)
Moves a file to a directory.
|
static String[] |
tail(File file,
int numberOfLinesToRead)
Returns the specified number of lines starting from the end
of a text file.
|
static String[] |
tail(File file,
String encoding,
int numberOfLinesToRead)
Returns the specified number of lines starting from the end
of a text file.
|
static String[] |
tail(File file,
String encoding,
int numberOfLinesToRead,
boolean stripBlankLines)
Returns the specified number of lines starting from the end
of a text file.
|
static String[] |
tail(File file,
String encoding,
int numberOfLinesToRead,
boolean stripBlankLines,
IInputStreamFilter filter)
Returns the specified number of lines starting from the end
of a text file.
|
static File |
toDateFormattedDir(File parentDir,
Date dateTime,
String format)
Gets (but does not create) a series of directories reflecting
the specified date format (from
SimpleDateFormat ),
under a given parent directory. |
static String |
toSafeFileName(String unsafeFileName)
Converts any String to a valid file-system file name representation.
|
static File |
toURLDir(File parentDir,
String url)
Gets (but does not create) a directory matching URL segments,
under a given parent directory.
|
static File |
toURLDir(File parentDir,
String url,
boolean truncate)
Gets (but does not create) a directory matching URL segments,
under a given parent directory.
|
static File |
toURLDir(File parentDir,
URL url)
Gets (but does not create) a directory matching URL segments,
under a given parent directory.
|
static File |
toURLDir(File parentDir,
URL url,
boolean truncate)
Gets (but does not create) a directory matching URL segments,
under a given parent directory.
|
static void |
visitAllDirs(File dir,
IFileVisitor visitor)
Visits only directories under a directory.
|
static void |
visitAllDirs(File dir,
IFileVisitor visitor,
FileFilter filter)
Visits only directories under a directory.
|
static void |
visitAllDirsAndFiles(File dir,
IFileVisitor visitor)
Visits all files and directories under a directory.
|
static void |
visitAllDirsAndFiles(File dir,
IFileVisitor visitor,
FileFilter filter)
Visits all files and directories under a directory.
|
static void |
visitAllFiles(File dir,
IFileVisitor visitor)
Visits all files (and only files) under a directory, including
sub-directories.
|
static void |
visitAllFiles(File dir,
IFileVisitor visitor,
FileFilter filter)
Visits all files (and only files) under a directory, including
sub-directories.
|
static void |
visitEmptyDirs(File dir,
IFileVisitor visitor)
Visits only empty directories under a directory.
|
static void |
visitEmptyDirs(File dir,
IFileVisitor visitor,
FileFilter filter)
Visits only empty directories under a directory.
|
public static boolean dirEmpty(File dir) throws IOException
dir
- the directory to check for emptinesstrue
if directory exists and is emptyIOException
- if an I/O error occurspublic static boolean dirHasFile(File dir, FileFilter filter) throws IOException
dir
- directory to inspectfilter
- file or directory filtertrue
upon filter matching a file or directoryIOException
- if an I/O error occurspublic static String toSafeFileName(String unsafeFileName)
fromSafeFileName(String)
to get back the original name.unsafeFileName
- the file name to make safe.public static String fromSafeFileName(String safeFileName)
toSafeFileName(String)
into its original string.safeFileName
- the file name to convert to its original formpublic static void moveFileToDir(File sourceFile, File targetDir) throws IOException
moveFile(File, File)
:
sourceFile
- source file to movetargetDir
- target destinationIOException
- cannot move file.public static void moveFile(File sourceFile, File targetFile) throws IOException
File.renameTo(File)
method in such that:
sourceFile
- source file to movetargetFile
- target destinationIOException
- cannot move file.public static void delete(File file) throws IOException
System.gc()
in hope of releasing any handles left on files. This is in
relation to a known Java bug mostly occurring on Windows
(http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4715154).null
or does not exist, nothing happens.
file
- file or directory to deleteIOException
- cannot delete file.deleteFile(File)
public static int deleteEmptyDirs(File parentDir)
parentDir
- the directory where to start looking for empty
directoriespublic static int deleteEmptyDirs(File parentDir, Date date)
null
, all empty
directories will be deleted, regardless of their date.parentDir
- the directory where to start looking for empty
directoriesdate
- the date to compare empty directories againstpublic static File createDirsForFile(File file) throws IOException
file
- the file to create parent directories forIOException
- if something went wrong creating the parent
directoriespublic static void visitAllDirsAndFiles(File dir, IFileVisitor visitor)
dir
- the directoryvisitor
- the visitorpublic static void visitAllDirsAndFiles(File dir, IFileVisitor visitor, FileFilter filter)
dir
- the directoryvisitor
- the visitorfilter
- an optional filter to restrict the files being visitedpublic static void visitEmptyDirs(File dir, IFileVisitor visitor)
dir
- the directoryvisitor
- the visitorpublic static void visitEmptyDirs(File dir, IFileVisitor visitor, FileFilter filter)
dir
- the directoryvisitor
- the visitorfilter
- an optional filter to restrict the visited directoriespublic static void visitAllDirs(File dir, IFileVisitor visitor)
dir
- the directoryvisitor
- the visitorpublic static void visitAllDirs(File dir, IFileVisitor visitor, FileFilter filter)
dir
- the directoryvisitor
- the visitorfilter
- an optional filter to restrict the visited directoriespublic static void visitAllFiles(File dir, IFileVisitor visitor)
dir
- the directoryvisitor
- the visitorpublic static void visitAllFiles(File dir, IFileVisitor visitor, FileFilter filter)
dir
- the directoryvisitor
- the visitorfilter
- an optional filter to restrict the files being visitedpublic static String[] head(File file, int numberOfLinesToRead) throws IOException
file
- the file to read lines fromnumberOfLinesToRead
- the number of lines to readIOException
- i/o problempublic static String[] head(File file, String encoding, int numberOfLinesToRead) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readIOException
- i/o problempublic static String[] head(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readstripBlankLines
- whether to return blank lines or notIOException
- i/o problempublic static String[] head(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines, IInputStreamFilter filter) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readstripBlankLines
- whether to return blank lines or notfilter
- InputStream filterIOException
- i/o problempublic static String[] tail(File file, int numberOfLinesToRead) throws IOException
file
- the file to read lines fromnumberOfLinesToRead
- the number of lines to readIOException
- i/o problempublic static String[] tail(File file, String encoding, int numberOfLinesToRead) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readIOException
- i/o problempublic static String[] tail(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readstripBlankLines
- whether to return blank lines or notIOException
- i/o problempublic static String[] tail(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines, IInputStreamFilter filter) throws IOException
file
- the file to read lines fromencoding
- the file encodingnumberOfLinesToRead
- the number of lines to readstripBlankLines
- whether to return blank lines or notfilter
- InputStream filterIOException
- i/o problempublic static File createDateDirs(File parentDir) throws IOException
/<parentDir>/2000/12/31/
parentDir
- the parent directory where to create date directoriesIOException
- if the parent directory is not validpublic static File createDateDirs(File parentDir, Date date) throws IOException
/<parentDir>/2000/12/31/
parentDir
- the parent directory where to create date directoriesdate
- the date to create directories fromIOException
- if the parent directory is not validpublic static File createDateTimeDirs(File parentDir) throws IOException
/<parentDir>/2000/12/31/13/34/12/
parentDir
- the parent directory where to create date directoriesIOException
- if the parent directory is not validpublic static File createDateTimeDirs(File parentDir, Date dateTime) throws IOException
/<parentDir>/2000/12/31/13/34/12/
parentDir
- the parent directory where to create date directoriesdateTime
- the date to create directories fromIOException
- if the parent directory is not validpublic static File createDateFormattedDirs(File parentDir, Date dateTime, String format) throws IOException
SimpleDateFormat
),
under a given parent directory.
Use forward slash in your date format for creating sub-directories.
For example,
a date of 2000-12-31T13:34:12 with a format of
yyyy/MM/dd/HH-mm-ss
will create the following directory
structure:
/<parentDir>/2000/12/31/13-34-12/
parentDir
- the parent directory where to create date directoriesdateTime
- the date to create directories fromformat
- the format to use for creating a date-formatted directoryIOException
- if the parent directory is not validpublic static File toDateFormattedDir(File parentDir, Date dateTime, String format)
SimpleDateFormat
),
under a given parent directory.
Use forward slash in your date format for creating sub-directories.
For example,
a date of 2000-12-31T13:34:12 with a format of
yyyy/MM/dd/HH-mm-ss
will create the following directory
structure:
/<parentDir>/2000/12/31/13-34-12/
parentDir
- the parent directory where to create date directoriesdateTime
- the date to create directories fromformat
- the format to use for creating a date-formatted directorypublic static File createURLDirs(File parentDir, URL url) throws IOException
Creates (if not already existing) a series of directories matching URL segments, under a given parent directory. The returned file contains the full path to the directories, plus the file name (not created). The file name is the last URL segment (including query string and fragment). Non-alphanumeric characters are escaped to be file-system-friendly.
Warning: the path created may be too long for some file systems.
To avoid issues with file names being too long, consider truncating
the generated path by using
createURLDirs(File, URL, boolean)
instead.
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.IOException
- if the parent directory is not validpublic static File createURLDirs(File parentDir, String url) throws IOException
Creates (if not already existing) a series of directories matching URL segments, under a given parent directory. The returned file contains the full path to the directories, plus the file name (not created). The file name is the last URL segment (including query string and fragment). Non-alphanumeric characters are escaped to be file-system-friendly.
Warning: the path created may be too long for some file systems.
To avoid issues with file names being too long, consider truncating
the generated path by using
createURLDirs(File, URL, boolean)
instead.
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.IOException
- if the parent directory is not validpublic static File createURLDirs(File parentDir, URL url, boolean truncate) throws IOException
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.truncate
- whether to truncate the directory to 255 characters max.IOException
- if the parent directory is not validpublic static File createURLDirs(File parentDir, String url, boolean truncate) throws IOException
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.truncate
- whether to truncate the directory to 255 characters max.IOException
- if the parent directory is not validpublic static File toURLDir(File parentDir, URL url)
Gets (but does not create) a directory matching URL segments, under a given parent directory. The returned file contains the full path to the directories, plus the file name. The file name is the last URL segment (including query string and fragment). Non-alphanumeric characters are escaped to be file-system-friendly.
Warning: the path created may be too long for some file systems.
To avoid issues with file names being too long, consider truncating
the generated path by using
toURLDir(File, URL, boolean)
instead.
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.public static File toURLDir(File parentDir, String url)
Gets (but does not create) a directory matching URL segments, under a given parent directory. The returned file contains the full path to the directories, plus the file name. The file name is the last URL segment (including query string and fragment). Non-alphanumeric characters are escaped to be file-system-friendly.
Warning: the path created may be too long for some file systems.
To avoid issues with file names being too long, consider truncating
the generated path by using
toURLDir(File, URL, boolean)
instead.
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.public static File toURLDir(File parentDir, URL url, boolean truncate)
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.truncate
- whether to truncate the directory to 255 characters max.public static File toURLDir(File parentDir, String url, boolean truncate)
parentDir
- the parent directory where to create URL directoriesurl
- the URL to create directories for, with file name.truncate
- whether to truncate the directory to 255 characters max.Copyright © 2008–2023 Norconex Inc.. All rights reserved.