Top Description Fields Constructors Methods
org.apache.commons.io

public Class FileUtils

extends Object
Class Inheritance
Imports
java.io.File, .FileFilter, .FileInputStream, .FileNotFoundException, .FileOutputStream, .IOException, .InputStream, .OutputStream, java.net.URL, java.util.Collection, .Date, .Iterator, .List, java.util.zip.CRC32, .CheckedInputStream, .Checksum, org.apache.commons.io.filefilter.DirectoryFileFilter, .FalseFileFilter, .FileFilterUtils, .IOFileFilter, .SuffixFileFilter, .TrueFileFilter, org.apache.commons.io.output.NullOutputStream

General file manipulation utilities.

Facilities are provided in the following areas:

Origin of code: Excalibur, Alexandria, Commons-Utils

Authors
Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner, Matthew Hawthorne, Jeremias Maerki, Stephen Colebourne, Ian Springer, Chris Eldredge, Jim Harrington, Niall Pemberton, Sandy McArthur
Version
$Id: FileUtils.java 507684 2007-02-14 20:38:25Z bayard $

Field Summary

Modifier and TypeField and Description
public static final File[]
EMPTY_FILE_ARRAY

An empty array of type File.

public static final long
ONE_GB

The number of bytes in a gigabyte.

public static final long
ONE_KB

The number of bytes in a kilobyte.

public static final long
ONE_MB

The number of bytes in a megabyte.

Constructor Summary

AccessConstructor and Description
public
FileUtils()

Instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

a human-readable display value (includes units)
byteCountToDisplaySize
(long
the number of bytes
size
)

Returns a human-readable version of the file size, where the input represents a specific number of bytes.

public static Checksum

Returns:

the checksum specified, updated with the content of the file
checksum
(File
the file to checksum, must not be null
file
,
Checksum
the checksum object to be used, must not be null
checksum
)

Computes the checksum of a file using the specified checksum object.

public static long

Returns:

the checksum value
checksumCRC32
(File
the file to checksum, must not be null
file
)

Computes the checksum of a file using the CRC32 checksum routine.

public static void
cleanDirectory(File
directory to clean
directory
)

Clean a directory without deleting it.

private static void
cleanDirectoryOnExit(File
directory to clean, must not be null
directory
)

Clean a directory without deleting it.

public static boolean

Returns:

true if the content of the files are equal or they both don't exist, false otherwise
contentEquals
(File
the first file
file1
,
File
the second file
file2
)

Compare the contents of two files to determine if they are equal or not.

public static File[]

Returns:

an array of java.io.File
convertFileCollectionToFileArray
(Collection<E>
a Collection containing java.io.File instances
files
)

Converts a Collection containing java.io.File instanced into array representation.

public static void
copyDirectory(File
an existing directory to copy, must not be null
srcDir
,
File
the new directory, must not be null
destDir
)

Copies a whole directory to a new location preserving the file dates.

public static void
copyDirectory(File
an existing directory to copy, must not be null
srcDir
,
File
the new directory, must not be null
destDir
,
boolean
true if the file date of the copy should be the same as the original
preserveFileDate
)

Copies a whole directory to a new location.

public static void
copyDirectoryToDirectory(File
an existing directory to copy, must not be null
srcDir
,
File
the directory to place the copy in, must not be null
destDir
)

Copies a directory to within another directory preserving the file dates.

public static void
copyFile(File
an existing file to copy, must not be null
srcFile
,
File
the new file, must not be null
destFile
)

Copies a file to a new location preserving the file date.

public static void
copyFile(File
an existing file to copy, must not be null
srcFile
,
File
the new file, must not be null
destFile
,
boolean
true if the file date of the copy should be the same as the original
preserveFileDate
)

Copies a file to a new location.

public static void
copyFileToDirectory(File
an existing file to copy, must not be null
srcFile
,
File
the directory to place the copy in, must not be null
destDir
)

Copies a file to a directory preserving the file date.

public static void
copyFileToDirectory(File
an existing file to copy, must not be null
srcFile
,
File
the directory to place the copy in, must not be null
destDir
,
boolean
true if the file date of the copy should be the same as the original
preserveFileDate
)

Copies a file to a directory optionally preserving the file date.

public static void
copyURLToFile(URL
the URL to copy bytes from, must not be null
source
,
File
the non-directory File to write bytes to (possibly overwriting), must not be null
destination
)

Copies bytes from the URL source to a file destination.

public static void
deleteDirectory(File
directory to delete
directory
)

Recursively delete a directory.

private static void
deleteDirectoryOnExit(File
directory to delete, must not be null
directory
)

Recursively schedule directory for deletion on JVM exit.

private static void
doCopyDirectory(File
the validated source directory, must not be null
srcDir
,
File
the validated destination directory, must not be null
destDir
,
boolean
whether to preserve the file date
preserveFileDate
)

Internal copy directory method.

private static void
doCopyFile(File
the validated source file, must not be null
srcFile
,
File
the validated destination file, must not be null
destFile
,
boolean
whether to preserve the file date
preserveFileDate
)

Internal copy file method.

public static void
forceDelete(File
file or directory to delete, must not be null
file
)

Delete a file.

public static void
forceDeleteOnExit(File
file or directory to delete, must not be null
file
)

Schedule a file to be deleted when JVM exits.

public static void
forceMkdir(File
directory to create, must not be null
directory
)

Make a directory, including any necessary but nonexistent parent directories.

private static void
innerListFiles(Collection<E>
the collection of files found.
files
,
File
the directory to search in.
directory
,
IOFileFilter
the filter to apply to files and directories.
filter
)

Finds files within a given directory (and optionally its subdirectories).

public static boolean

Returns:

true if the File exists and has been modified more recently than the reference File
isFileNewer
(File
the File of which the modification date must be compared, must not be null
file
,
File
the File of which the modification date is used, must not be null
reference
)

Tests if the specified File is newer than the reference File.

public static boolean

Returns:

true if the File exists and has been modified after the given Date.
isFileNewer
(File
the File of which the modification date must be compared, must not be null
file
,
Date
the date reference, must not be null
date
)

Tests if the specified File is newer than the specified Date.

public static boolean

Returns:

true if the File exists and has been modified after the given time reference.
isFileNewer
(File
the File of which the modification date must be compared, must not be null
file
,
long
the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
timeMillis
)

Tests if the specified File is newer than the specified time reference.

public static boolean

Returns:

true if the File exists and has been modified before the reference File
isFileOlder
(File
the File of which the modification date must be compared, must not be null
file
,
File
the File of which the modification date is used, must not be null
reference
)

Tests if the specified File is older than the reference File.

public static boolean

Returns:

true if the File exists and has been modified before the given Date.
isFileOlder
(File
the File of which the modification date must be compared, must not be null
file
,
Date
the date reference, must not be null
date
)

Tests if the specified File is older than the specified Date.

public static boolean

Returns:

true if the File exists and has been modified before the given time reference.
isFileOlder
(File
the File of which the modification date must be compared, must not be null
file
,
long
the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
timeMillis
)

Tests if the specified File is older than the specified time reference.

public static Iterator<E>

Returns:

an iterator of java.io.File for the matching files
iterateFiles
(File
the directory to search in
directory
,
IOFileFilter
filter to apply when finding files.
fileFilter
,
IOFileFilter
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.
dirFilter
)

Allows iteration over the files in given directory (and optionally its subdirectories).

public static Iterator<E>

Returns:

an iterator of java.io.File with the matching files
iterateFiles
(File
the directory to search in
directory
,
String[]
an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
extensions
,
boolean
if true all subdirectories are searched as well
recursive
)

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

public static LineIterator

Returns:

an Iterator of the lines in the file, never null
lineIterator
(File
the file to open for input, must not be null
file
,
String
the encoding to use, null means platform default
encoding
)

Return an Iterator for the lines in a File.

public static LineIterator

Returns:

an Iterator of the lines in the file, never null
lineIterator
(File
the file to open for input, must not be null
file
)

Return an Iterator for the lines in a File using the default encoding for the VM.

public static Collection<E>

Returns:

an collection of java.io.File with the matching files
listFiles
(File
the directory to search in
directory
,
IOFileFilter
filter to apply when finding files.
fileFilter
,
IOFileFilter
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.
dirFilter
)

Finds files within a given directory (and optionally its subdirectories).

public static Collection<E>

Returns:

an collection of java.io.File with the matching files
listFiles
(File
the directory to search in
directory
,
String[]
an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
extensions
,
boolean
if true all subdirectories are searched as well
recursive
)

Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.

public static FileInputStream

Returns:

a new FileInputStream for the specified file
openInputStream
(File
the file to open for input, must not be null
file
)

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

public static FileOutputStream

Returns:

a new FileOutputStream for the specified file
openOutputStream
(File
the file to open for output, must not be null
file
)

Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

public static byte[]

Returns:

the file contents, never null
readFileToByteArray
(File
the file to read, must not be null
file
)

Reads the contents of a file into a byte array.

public static String

Returns:

the file contents, never null
readFileToString
(File
the file to read, must not be null
file
,
String
the encoding to use, null means platform default
encoding
)

Reads the contents of a file into a String.

public static String

Returns:

the file contents, never null
readFileToString
(File
the file to read, must not be null
file
)

Reads the contents of a file into a String using the default encoding for the VM.

public static List<E>

Returns:

the list of Strings representing each line in the file, never null
readLines
(File
the file to read, must not be null
file
,
String
the encoding to use, null means platform default
encoding
)

Reads the contents of a file line by line to a List of Strings.

public static List<E>

Returns:

the list of Strings representing each line in the file, never null
readLines
(File
the file to read, must not be null
file
)

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

public static long

Returns:

size of directory in bytes, 0 if directory is security restricted
sizeOfDirectory
(File
directory to inspect, must not be null
directory
)

Recursively count size of a directory (sum of the length of all files).

public static File

Returns:

the equivalent File object, or null if the URL's protocol is not file
toFile
(URL
the file URL to convert, null returns null
url
)

Convert from a URL to a File.

public static File[]

Returns:

a non-null array of Files matching the input, with a null item if there was a null at that index in the input array
toFiles
(URL[]
the file URLs to convert, null returns empty array
urls
)

Converts each of an array of URL to a File.

private static String[]

Returns:

an array of suffixes. Format: {".java", ".xml"}
toSuffixes
(String[]
an array of extensions. Format: {"java", "xml"}
extensions
)

Converts an array of file extensions to suffixes for use with IOFileFilters.

public static void
touch(File
the File to touch
file
)

Implements the same behaviour as the "touch" utility on Unix.

public static URL[]

Returns:

an array of URLs matching the input
toURLs
(File[]
the files to convert
files
)

Converts each of an array of File to a URL.

public static boolean

Returns:

true if file exists
waitFor
(File
the file to check, must not be null
file
,
int
the maximum time in seconds to wait
seconds
)

Waits for NFS to propagate a file creation, imposing a timeout.

public static void
writeByteArrayToFile(File
the file to write to
file
,
byte[]
the content to write to the file
data
)

Writes a byte array to a file creating the file if it does not exist.

public static void
writeLines(File
the file to write to
file
,
String
the encoding to use, null means platform default
encoding
,
Collection<E>
the lines to write, null entries produce blank lines
lines
)

Writes the toString() value of each item in a collection to the specified File line by line.

public static void
writeLines(File
the file to write to
file
,
Collection<E>
the lines to write, null entries produce blank lines
lines
)

Writes the toString() value of each item in a collection to the specified File line by line.

public static void
writeLines(File
the file to write to
file
,
String
the encoding to use, null means platform default
encoding
,
Collection<E>
the lines to write, null entries produce blank lines
lines
,
String
the line separator to use, null is system default
lineEnding
)

Writes the toString() value of each item in a collection to the specified File line by line.

public static void
writeLines(File
the file to write to
file
,
Collection<E>
the lines to write, null entries produce blank lines
lines
,
String
the line separator to use, null is system default
lineEnding
)

Writes the toString() value of each item in a collection to the specified File line by line.

public static void
writeStringToFile(File
the file to write
file
,
String
the content to write to the file
data
,
String
the encoding to use, null means platform default
encoding
)

Writes a String to a file creating the file if it does not exist.

public static void
writeStringToFile(File
the file to write
file
,
String
the content to write to the file
data
)

Writes a String to a file creating the file if it does not exist using the default encoding for the VM.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait