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 |
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 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 String |
toSafeFileName(String unsafeFileName)
Converts any String to a valid file-system file name representation.
|
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 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 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 date 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 date 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 date 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 date 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 validCopyright © 2008–2021 Norconex Inc.. All rights reserved.