|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.io.FileUtils
public class FileUtils
This class provides basic facilities for manipulating files and file paths.
Methods exist to retrieve the components of a typical file path. For example
/www/hosted/mysite/index.html
, can be broken into:
/www/hosted/mysite/
-- retrievable through #getPath
index.html
-- retrievable through #removePath
/www/hosted/mysite/index
-- retrievable through #removeExtension
html
-- retrievable through #getExtension
concatenate two paths
, resolve a
path relative to a File
and #normalize
a path.
There are methods to create a File from a URL
, copy a
File to a directory
,
copy a File to another File
,
copy a URL's contents to a File
,
as well as methods to delete
and clean
a directory.
File
manipulation routines.
Field Summary | |
---|---|
static long |
ONE_GB
The number of bytes in a gigabyte. |
static long |
ONE_KB
The number of bytes in a kilobyte. |
static long |
ONE_MB
The number of bytes in a megabyte. |
Constructor Summary | |
---|---|
FileUtils()
Instances should NOT be constructed in standard programming. |
Method Summary | |
---|---|
static String |
byteCountToDisplaySize(long size)
Returns a human-readable version of the file size (original is in bytes). |
static void |
cleanDirectory(File directory)
Clean a directory without deleting it. |
static boolean |
contentEquals(File file1,
File file2)
Compare the contents of two files to determine if they are equal or not. |
static File[] |
convertFileCollectionToFileArray(Collection files)
Converts a Collection containing java.io.File instanced into array representation. |
static void |
copyFile(File source,
File destination)
Copy file from source to destination. |
static void |
copyFile(File source,
File destination,
boolean preserveFileDate)
Copy file from source to destination. |
static void |
copyFileToDirectory(File source,
File destinationDirectory)
Copy file from source to destination. |
static void |
copyURLToFile(URL source,
File destination)
Copies bytes from the URL source to a file destination . |
static void |
deleteDirectory(File directory)
Recursively delete a directory. |
static void |
forceDelete(File file)
Delete a file. |
static void |
forceDeleteOnExit(File file)
Schedule a file to be deleted when JVM exits. |
static void |
forceMkdir(File directory)
Make a directory. |
static boolean |
isFileNewer(File file,
Date date)
Tests if the specified File is newer than the specified
Date |
static boolean |
isFileNewer(File file,
File reference)
Tests if the specified File is newer than the reference
File . |
static boolean |
isFileNewer(File file,
long timeMillis)
Tests if the specified File is newer than the specified
time reference. |
static Collection |
listFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its subdirectories). |
static Collection |
listFiles(File directory,
String[] extensions,
boolean recursive)
Finds files within a given directory (and optionally its subdirectories) which match an array of extensions. |
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String. |
static long |
sizeOfDirectory(File directory)
Recursively count size of a directory (sum of the length of all files). |
static File |
toFile(URL url)
Convert from a URL to a File . |
static void |
touch(File file)
Implements the same behaviour as the "touch" utility on Unix. |
static URL[] |
toURLs(File[] files)
Convert the array of Files into a list of URLs. |
static boolean |
waitFor(File file,
int seconds)
Waits for NFS to propagate a file creation, imposing a timeout. |
static void |
writeStringToFile(File file,
String data,
String encoding)
Writes data to a file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
Constructor Detail |
---|
public FileUtils()
Method Detail |
---|
public static String byteCountToDisplaySize(long size)
size
- The number of bytes.
public static void touch(File file) throws IOException
file
- the File to touch
IOException
- If an I/O problem occurspublic static File[] convertFileCollectionToFileArray(Collection files)
files
- a Collection containing java.io.File instances
public static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.
If your search should recurse into subdirectories you can pass in an IOFileFilter for directories. You don't need to bind a DirectoryFileFilter (via logical AND) to this filter. This method does that for you.
An example: If you want to search through all directories called
"temp" you pass in FileFilterUtils.NameFileFilter("temp")
Another common usage of this method is find files in a directory
tree but ignoring the directories generated CVS. You can simply pass
in FileFilterUtils.makeCVSAware(null)
.
directory
- the directory to search infileFilter
- filter to apply when finding files.dirFilter
- optional filter to apply when finding subdirectories.
If this parameter is null, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.
FileFilterUtils
,
NameFileFilter
public static Collection listFiles(File directory, String[] extensions, boolean recursive)
directory
- the directory to search inextensions
- an array of extensions, ex. {"java","xml"}. If this
parameter is null, all files are returned.recursive
- If true all subdirectories are searched, too.
public static boolean contentEquals(File file1, File file2) throws IOException
Compare the contents of two files to determine if they are equal or not.
Code origin: Avalon
file1
- the first filefile2
- the second file
IOException
- in case of an I/O errorpublic static File toFile(URL url)
URL
to a File
.
url
- File URL.
File
object, or null
if the URL's protocol
is not file
public static URL[] toURLs(File[] files) throws IOException
files
- the array of files
IOException
- if an error occurspublic static void copyFileToDirectory(File source, File destinationDirectory) throws IOException
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
The copy will have the same file date as the original.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
FileNotFoundException
- if source
isn't a normal file.
IllegalArgumentException
- if destinationDirectory
isn't a directory.
IOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error occurs during copying.public static void copyFile(File source, File destination) throws IOException
destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
The copy will have the same file date as the original.
source
- An existing non-directory File
to copy
bytes from.destination
- A non-directory File
to write bytes to
(possibly overwriting).
IOException
- if source
does not exist, destination
cannot be
written to, or an IO error occurs during copying.
FileNotFoundException
- if destination
is a directory
(use copyFileToDirectory(java.io.File, java.io.File)
).public static void copyFile(File source, File destination, boolean preserveFileDate) throws IOException
destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
source
- An existing non-directory File
to copy
bytes from.destination
- A non-directory File
to write bytes to
(possibly overwriting).preserveFileDate
- True if the file date of the copy should be the
same as the original.
IOException
- if source
does not exist, destination
cannot be
written to, or an IO error occurs during copying.
FileNotFoundException
- if destination
is a directory
(use copyFileToDirectory(java.io.File, java.io.File)
).public static void copyURLToFile(URL source, File destination) throws IOException
source
to a file destination
.
The directories up to destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
source
- A URL
to copy bytes from.destination
- A non-directory File
to write bytes to (possibly
overwriting).
IOException
- if
source
URL cannot be openeddestination
cannot be written topublic static void deleteDirectory(File directory) throws IOException
directory
- directory to delete
IOException
- in case deletion is unsuccessfulpublic static void cleanDirectory(File directory) throws IOException
directory
- directory to clean
IOException
- in case cleaning is unsuccessfulpublic static boolean waitFor(File file, int seconds)
file
- The fileseconds
- The maximum time in seconds to wait.
public static String readFileToString(File file, String encoding) throws IOException
Reads the contents of a file into a String.
There is no readFileToString method without encoding parameter because the default encoding can differ between platforms and therefore results in inconsistent results.
file
- the file to read.encoding
- the encoding to use
IOException
- in case of an I/O error
UnsupportedEncodingException
- if the encoding is not supported
by the VMpublic static void writeStringToFile(File file, String data, String encoding) throws IOException
Writes data to a file. The file will be created if it does not exist.
There is no readFileToString method without encoding parameter because the default encoding can differ between platforms and therefore results in inconsistent results.
file
- the file to write.data
- The content to write to the file.encoding
- encoding to use
IOException
- in case of an I/O error
UnsupportedEncodingException
- if the encoding is not supported
by the VMpublic static void forceDelete(File file) throws IOException
Delete a file. If file is a directory, delete it and all sub-directories.
The difference between File.delete() and this method are:
file
- file or directory to delete.
IOException
- in case deletion is unsuccessfulpublic static void forceDeleteOnExit(File file) throws IOException
file
- file or directory to delete.
IOException
- in case deletion is unsuccessfulpublic static void forceMkdir(File directory) throws IOException
directory
- directory to create
IOException
- if the directory cannot be created.public static long sizeOfDirectory(File directory)
directory
- directory to inspect
public static boolean isFileNewer(File file, File reference)
File
is newer than the reference
File
.
file
- the File
of which the modification date must be comparedreference
- the File
of which the modification date is used
like reference
File
exists and has been modified more recently
than the reference File
.public static boolean isFileNewer(File file, Date date)
File
is newer than the specified
Date
file
- the File
of which the modification date must be compareddate
- the date reference
File
exists and has been modified after
the given Date
.public static boolean isFileNewer(File file, long timeMillis)
File
is newer than the specified
time reference.
file
- the File
of which the modification date must be compared.timeMillis
- the time reference measured in milliseconds since the epoch
(00:00:00 GMT, January 1, 1970)
File
exists and has been modified after
the given time reference.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |