org.codehaus.plexus.util
public class FileUtils extends Object
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 FileUtilsindex.html
-- retrievable through FileUtils/www/hosted/mysite/index
-- retrievable through FileUtilshtml
-- retrievable through FileUtilsconcatenate two paths
, resolve a
path relative to a File
and FileUtils a path.
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.
Common java.io.File manipulation routines.
Taken from the commons-utils repo.
Also code from Alexandria's FileUtils.
And from Avalon Excalibur's IO.
And from Ant.
Version: $Id: FileUtils.java 8145 2009-03-25 19:54:54Z bentmann $
Nested Class Summary | |
---|---|
abstract static class | FileUtils.FilterWrapper |
Field Summary | |
---|---|
static String | FS The vm line separator |
static int | ONE_GB
The number of bytes in a gigabyte. |
static int | ONE_KB
The number of bytes in a kilobyte. |
static int | ONE_MB
The number of bytes in a megabyte. |
Method Summary | |
---|---|
static String | basename(String filename)
Returns the filename portion of a file specification string.
|
static String | basename(String filename, String suffix)
Returns the filename portion of a file specification string.
|
static String | byteCountToDisplaySize(int size)
Returns a human-readable version of the file size (original is in
bytes).
|
static String | catPath(String lookupPath, String path)
Will concatenate 2 paths. |
static void | cleanDirectory(String directory)
Clean a directory without deleting it.
|
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 void | copyDirectory(File sourceDirectory, File destinationDirectory)
Copy a directory to an other one.
|
static void | copyDirectory(File sourceDirectory, File destinationDirectory, String includes, String excludes)
Copy a directory to an other one.
|
static void | copyDirectoryLayout(File sourceDirectory, File destinationDirectory, String[] includes, String[] excludes)
Copies a entire directory layout : no files will be copied only directories
Note:
|
static void | copyDirectoryStructure(File sourceDirectory, File destinationDirectory)
Copies a entire directory structure.
|
static void | copyDirectoryStructureIfModified(File sourceDirectory, File destinationDirectory)
Copies an entire directory structure but only source files with timestamp later than the destinations'.
|
static void | copyFile(File source, File destination)
Copy file from source to destination. |
static void | copyFile(File from, File to, String encoding, FileUtils.FilterWrapper[] wrappers)
If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() |
static void | copyFile(File from, File to, String encoding, FileUtils.FilterWrapper[] wrappers, boolean overwrite)
If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if overwrite is true |
static boolean | copyFileIfModified(File source, File destination)
Copy file from source to destination only if source timestamp is later than the destination timestamp.
|
static void | copyFileToDirectory(String source, String destinationDirectory)
Copy file from source to destination. |
static void | copyFileToDirectory(File source, File destinationDirectory)
Copy file from source to destination. |
static void | copyFileToDirectoryIfModified(String source, String destinationDirectory)
Copy file from source to destination only if source is newer than the target file.
|
static void | copyFileToDirectoryIfModified(File source, File destinationDirectory)
Copy file from source to destination only if source is newer than the target file.
|
static void | copyStreamToFile(InputStreamFacade source, File destination)
Copies bytes from the InputStream source to a file destination .
|
static void | copyURLToFile(URL source, File destination)
Copies bytes from the URL source to a file destination .
|
static File | createTempFile(String prefix, String suffix, File parentDir)
Create a temporary file in a given directory.
|
static void | deleteDirectory(String directory)
Recursively delete a directory.
|
static void | deleteDirectory(File directory)
Recursively delete a directory.
|
static String | dirname(String filename)
Returns the directory path portion of a file specification string.
|
static String | extension(String filename)
Returns the extension portion of a file specification string.
|
static void | fileAppend(String fileName, String data)
Appends data to a file. |
static void | fileAppend(String fileName, String encoding, String data)
Appends data to a file. |
static void | fileDelete(String fileName)
Deletes a file.
|
static boolean | fileExists(String fileName)
Check if a file exits.
|
static String | filename(String filename)
Returns the filename portion of a file specification string.
|
static String | fileRead(String file)
Note: the file content is read with platform encoding.
|
static String | fileRead(String file, String encoding) |
static String | fileRead(File file)
Note: the file content is read with platform encoding
|
static String | fileRead(File file, String encoding) |
static void | fileWrite(String fileName, String data)
Writes data to a file. |
static void | fileWrite(String fileName, String encoding, String data)
Writes data to a file. |
static void | forceDelete(String file)
Delete a file. |
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 file)
Make a directory.
|
static String[] | getDefaultExcludes() |
static List | getDefaultExcludesAsList() |
static String | getDefaultExcludesAsString() |
static List | getDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir)
Return a list of directories as String depending options.
|
static List | getDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive)
Return a list of directories as String depending options.
|
static String | getExtension(String filename)
Get extension from filename.
ie
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> "" |
static File | getFile(String fileName)
Creates a file handle.
|
static List | getFileAndDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories)
Return a list of files as String depending options.
|
static List | getFileNames(File directory, String includes, String excludes, boolean includeBasedir)
Return a list of files as String depending options.
|
static List | getFileNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive)
Return a list of files as String depending options.
|
static List | getFiles(File directory, String includes, String excludes)
Return the files contained in the directory, using inclusion and exclusion Ant patterns,
including the directory name in each of the files
|
static List | getFiles(File directory, String includes, String excludes, boolean includeBasedir)
Return the files contained in the directory, using inclusion and exclusion Ant patterns
|
static String[] | getFilesFromExtension(String directory, String[] extensions)
Given a directory and an array of extensions return an array of compliant files.
|
static String | getPath(String filepath)
Get path from filename. |
static String | getPath(String filepath, char fileSeparatorChar)
Get path from filename.
ie.
|
static boolean | isValidWindowsFileName(File f)
For Windows OS, check if the file name contains any of the following characters:
":", "*", "? |
static List | loadFile(File file)
Note: the file content is read with platform encoding
|
static void | mkdir(String dir)
Simple way to make a directory
|
static String | normalize(String path)
Normalize a path.
|
static String | removeExtension(String filename)
Remove extension from filename.
ie
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c |
static String | removePath(String filepath)
Remove path from filename. |
static String | removePath(String filepath, char fileSeparatorChar)
Remove path from filename.
ie.
|
static void | rename(File from, File to)
Renames a file, even if that involves crossing file system boundaries.
|
static File | resolveFile(File baseFile, String filename)
Resolve a file filename to it's canonical form. |
static long | sizeOfDirectory(String directory)
Recursively count size of a directory.
|
static long | sizeOfDirectory(File directory)
Recursively count size of a directory.
|
static File | toFile(URL url)
Convert from a URL to a File .
|
static URL[] | toURLs(File[] files)
Convert the array of Files into a list of URLs.
|
static boolean | waitFor(String fileName, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
|
static boolean | waitFor(File file, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
|
Parameters: filename the file path
Returns: The filename string without extension.
Parameters: filename the file path suffix the file suffix
Returns: the basename of the file
Parameters: size The number of bytes.
Returns: A human-readable display value (includes units).
..
will be
properly handled.
Eg.,
/a/b/c
+ d
= /a/b/d
/a/b/c
+ ../d
= /a/d
Parameters: lookupPath a path path the path to concatenate
Returns: The concatenated paths, or null if error occurs
Parameters: directory a directory
Throws: IOException if any
Parameters: directory a directory
Throws: IOException if any
Parameters: file1 the first file file2 the second file
Returns: true if the content of the files are equal or they both don't exist, false otherwise
Throws: IOException if any
Parameters: sourceDirectory the source dir destinationDirectory the target dir
Throws: IOException if any
Parameters: sourceDirectory the source dir destinationDirectory the target dir includes include pattern excludes exlucde pattern
Throws: IOException if any
See Also: FileUtils
sourceDirectory
must exists.
Parameters: sourceDirectory the source dir destinationDirectory the target dir includes include pattern excludes exlucde pattern
Throws: IOException if any
Since: 1.5.7
sourceDirectory
must exists.
Parameters: sourceDirectory the source dir destinationDirectory the target dir
Throws: IOException if any
sourceDirectory
must exists.
Parameters: sourceDirectory the source dir destinationDirectory the target dir
Throws: IOException if any
destination
will be
created if they don't already exist. destination
will be overwritten if it
already exists.
Parameters: source An existing non-directory File
to copy bytes from. destination A non-directory File
to write bytes to (possibly
overwriting).
Throws: IOException if source
does not exist, destination
cannot be
written to, or an IO error occurs during copying. java.io.FileNotFoundException if destination
is a directory
(use FileUtils).
Parameters: from the file to copy to the destination file encoding the file output encoding (only if wrappers is not empty) wrappers array of FilterWrapper
Throws: IOException if an IO error occurs during copying or filtering
Parameters: from the file to copy to the destination file encoding the file output encoding (only if wrappers is not empty) wrappers array of FilterWrapper overwrite if true and f wrappers is null or empty, the file will be copy enven if to.lastModified() < from.lastModified()
Throws: IOException if an IO error occurs during copying or filtering
Since: 1.5.2
destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
Parameters: source An existing non-directory File
to copy bytes from. destination A non-directory File
to write bytes to (possibly
overwriting).
Returns: true if no problem occured
Throws: 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 FileUtils).
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
Parameters: source An existing File
to copy. destinationDirectory A directory to copy source
into.
Throws: java.io.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.
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
Parameters: source An existing File
to copy. destinationDirectory A directory to copy source
into.
Throws: java.io.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.
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
Parameters: source An existing File
to copy. destinationDirectory A directory to copy source
into.
Throws: java.io.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.
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
Parameters: source An existing File
to copy. destinationDirectory A directory to copy source
into.
Throws: java.io.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.
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.
Parameters: source An InputStream to copy bytes from. This stream is
guaranteed to be closed. destination A non-directory File
to write bytes to (possibly
overwriting).
Throws: IOException if
source
URL cannot be openeddestination
cannot be written to
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.
Parameters: source A URL
to copy bytes from. destination A non-directory File
to write bytes to (possibly
overwriting).
Throws: IOException if
source
URL cannot be openeddestination
cannot be written to
The file denoted by the returned abstract pathname did not exist before this method was invoked, any subsequent invocation of this method will yield a different file name.
The filename is prefixNNNNNsuffix where NNNN is a random numberThis method is different to File#createTempFile(String, String, File) of JDK 1.2 as it doesn't create the file itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is null.
To delete automatically the file created by this method, use the File#deleteOnExit() method.
Parameters: prefix prefix before the random number suffix file extension; include the '.' parentDir Directory to create the temporary file in -java.io.tmpdir
used if not specificed
Returns: a File reference to the new temporary file.
Parameters: directory a directory
Throws: IOException if any
Parameters: directory a directory
Throws: IOException if any
Parameters: filename the file path
Returns: The directory portion excluding the ending file separator.
Parameters: filename the file path
Returns: the extension of the file
Parameters: fileName The path of the file to write. data The content to write to the file.
Throws: IOException if any
Parameters: fileName The path of the file to write. encoding The encoding of the file. data The content to write to the file.
Throws: IOException if any
Parameters: fileName The path of the file to delete.
Parameters: fileName the file path.
Returns: true if file exists.
Parameters: filename the file path
Returns: The filename string with extension.
Parameters: file the file path
Returns: the file content using the platform encoding.
Throws: IOException if any
Parameters: file the file path encoding the wanted encoding
Returns: the file content using the specified encoding.
Throws: IOException if any
Parameters: file the file path
Returns: the file content using the platform encoding.
Throws: IOException if any
Parameters: file the file path encoding the wanted encoding
Returns: the file content using the specified encoding.
Throws: IOException if any
Parameters: fileName The path of the file to write. data The content to write to the file.
Throws: IOException if any
Parameters: fileName The path of the file to write. encoding The encoding of the file. data The content to write to the file.
Throws: IOException if any
Parameters: file the file path
Throws: IOException if any
Parameters: file a file
Throws: IOException if any
Parameters: file a file
Throws: IOException if any
Parameters: file not null
Throws: IOException If there already exists a file with specified name or the directory is unable to be created IllegalArgumentException if the file contains illegal Windows characters under Windows OS.
See Also: INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
Returns: the default excludes pattern
See Also: DirectoryScanner
Returns: the default excludes pattern as list.
See Also: getDefaultExcludes
Returns: the default excludes pattern as comma separated string.
See Also: DirectoryScanner (Object[], String)
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each String of file
Returns: a list of directories as String
Throws: IOException
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each String of file isCaseSensitive true if case sensitive
Returns: a list of directories as String
Throws: IOException
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
Parameters: filename the path of the file
Returns: the extension of filename or "" if none
Parameters: fileName The path of the file.
Returns: A File
manager.
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each String of file isCaseSensitive true if case sensitive getFiles true if get files getDirectories true if get directories
Returns: a list of files as String
Throws: IOException
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each String of file
Returns: a list of files as String
Throws: IOException
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each String of file isCaseSensitive true if case sensitive
Returns: a list of files as String
Throws: IOException
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated
Returns: a list of File objects
Throws: IOException
See Also: FileUtils
Parameters: directory the directory to scan includes the includes pattern, comma separated excludes the excludes pattern, comma separated includeBasedir true to include the base dir in each file
Returns: a list of File objects
Throws: IOException
See Also: FileUtils
Parameters: directory The path of the directory. extensions an array of expected extensions.
Returns: An array of files for the wanted extensions.
dirname
.
ie.
a/b/c.txt --> a/b a.txt --> ""
Parameters: filepath the filepath
Returns: the filename minus path
a/b/c.txt --> a/b a.txt --> ""
Parameters: filepath the filepath fileSeparatorChar the file separator character like / on Unix plateforms.
Returns: the filename minus path
":", "*", "?", "\"", "<", ">", "|"
Parameters: f not null file
Returns: false
if the file path contains any of forbidden Windows characters,
true
if the Os is not Windows or if the file path respect the Windows constraints.
Since: 1.5.2
See Also: INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
Parameters: file the file
Returns: a List containing every every line not starting with # and not empty
Throws: IOException if any
Parameters: dir the directory to create
Throws: IllegalArgumentException if the dir contains illegal Windows characters under Windows OS.
See Also: INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
null
if the ..'s went past the
root.
Eg:
/foo// --> /foo/ /foo/./ --> /foo/ /foo/../bar --> /bar /foo/../bar/ --> /bar/ /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null
Parameters: path the path to normalize
Returns: the normalized String, or null
if too many ..'s.
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
Parameters: filename the path of the file
Returns: the filename minus extension
basename
ie.
a/b/c.txt --> c.txt a.txt --> a.txt
Parameters: filepath the path of the file
Returns: the filename minus path
a/b/c.txt --> c.txt a.txt --> a.txt
Parameters: filepath the path of the file fileSeparatorChar the file separator character like / on Unix plateforms.
Returns: the filename minus path
This will remove to
(if it exists), ensure that
to
's parent directory exists and move
from
, which involves deleting from
as
well.
Parameters: from the file to move to the new file name
Throws: IOException if anything bad happens during this process.
Note that to
may have been deleted already when this happens.
filename
to it's canonical form. If filename
is
relative (doesn't start with /
), it will be resolved relative to
baseFile
, otherwise it is treated as a normal root-relative path.
Parameters: baseFile Where to resolve filename
from, if filename
is
relative. filename Absolute or relative file path to resolve.
Returns: The canonical File
of filename
.
Parameters: directory a directory
Returns: size of directory in bytes.
Parameters: directory a directory
Returns: size of directory in bytes.
URL
to a File
.
Parameters: url File URL.
Returns: The equivalent File
object, or null
if the URL's protocol
is not file
Parameters: files the array of files
Returns: the array of URLs
Throws: IOException if an error occurs
Parameters: fileName The path of the file. seconds The maximum time in seconds to wait.
Returns: True if file exists.
Parameters: file The file. seconds The maximum time in seconds to wait.
Returns: True if file exists.