|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.plexus.util.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(java.lang.String)
index.html
-- retrievable through removePath(java.lang.String)
/www/hosted/mysite/index
-- retrievable through removeExtension(java.lang.String)
html
-- retrievable through getExtension(java.lang.String)
concatenate two paths
, resolve a
path relative to a File
and normalize(java.lang.String)
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 File
manipulation routines.
Taken from the commons-utils repo.
Also code from Alexandria's FileUtils.
And from Avalon Excalibur's IO.
And from Ant.
Nested Class Summary | |
static class |
FileUtils.FilterWrapper
|
Field Summary | |
static java.lang.String |
FS
|
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. |
Constructor Summary | |
FileUtils()
|
Method Summary | |
static java.lang.String |
basename(java.lang.String filename)
Returns the filename portion of a file specification string. |
static java.lang.String |
basename(java.lang.String filename,
java.lang.String suffix)
Returns the filename portion of a file specification string. |
static java.lang.String |
byteCountToDisplaySize(int size)
Returns a human-readable version of the file size (original is in bytes). |
static java.lang.String |
catPath(java.lang.String lookupPath,
java.lang.String path)
Will concatenate 2 paths. |
static void |
cleanDirectory(java.io.File directory)
Clean a directory without deleting it. |
static void |
cleanDirectory(java.lang.String directory)
Clean a directory without deleting it. |
static boolean |
contentEquals(java.io.File file1,
java.io.File file2)
Compare the contents of two files to determine if they are equal or not. |
static void |
copyDirectory(java.io.File sourceDirectory,
java.io.File destinationDirectory)
|
static void |
copyDirectory(java.io.File sourceDirectory,
java.io.File destinationDirectory,
java.lang.String includes,
java.lang.String excludes)
|
static void |
copyDirectoryStructure(java.io.File sourceDirectory,
java.io.File destinationDirectory)
Copies a entire directory structure. |
static void |
copyDirectoryStructureIfModified(java.io.File sourceDirectory,
java.io.File destinationDirectory)
Copies an entire directory structure but only source files with timestamp later than the destinations'. |
static void |
copyFile(java.io.File source,
java.io.File destination)
Copy file from source to destination. |
static void |
copyFile(java.io.File from,
java.io.File to,
java.lang.String encoding,
FileUtils.FilterWrapper[] wrappers)
|
static boolean |
copyFileIfModified(java.io.File source,
java.io.File destination)
Copy file from source to destination only if source timestamp is later than the destination timestamp. |
static void |
copyFileToDirectory(java.io.File source,
java.io.File destinationDirectory)
Copy file from source to destination. |
static void |
copyFileToDirectory(java.lang.String source,
java.lang.String destinationDirectory)
Copy file from source to destination. |
static void |
copyFileToDirectoryIfModified(java.io.File source,
java.io.File destinationDirectory)
Copy file from source to destination only if source is newer than the target file. |
static void |
copyFileToDirectoryIfModified(java.lang.String source,
java.lang.String destinationDirectory)
Copy file from source to destination only if source is newer than the target file. |
static void |
copyURLToFile(java.net.URL source,
java.io.File destination)
Copies bytes from the URL source to a file destination . |
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File parentDir)
Create a temporary file in a given directory. |
static void |
deleteDirectory(java.io.File directory)
Recursively delete a directory. |
static void |
deleteDirectory(java.lang.String directory)
Recursively delete a directory. |
static java.lang.String |
dirname(java.lang.String filename)
Returns the directory path portion of a file specification string. |
static java.lang.String |
extension(java.lang.String filename)
Returns the extension portion of a file specification string. |
static void |
fileAppend(java.lang.String fileName,
java.lang.String data)
Appends data to a file. |
static void |
fileAppend(java.lang.String fileName,
java.lang.String encoding,
java.lang.String data)
Appends data to a file. |
static void |
fileDelete(java.lang.String fileName)
Deletes a file. |
static boolean |
fileExists(java.lang.String fileName)
Check if a file exits. |
static java.lang.String |
filename(java.lang.String filename)
Returns the filename portion of a file specification string. |
static java.lang.String |
fileRead(java.io.File file)
|
static java.lang.String |
fileRead(java.io.File file,
java.lang.String encoding)
|
static java.lang.String |
fileRead(java.lang.String file)
|
static java.lang.String |
fileRead(java.lang.String file,
java.lang.String encoding)
|
static void |
fileWrite(java.lang.String fileName,
java.lang.String data)
Writes data to a file. |
static void |
fileWrite(java.lang.String fileName,
java.lang.String encoding,
java.lang.String data)
Writes data to a file. |
static void |
forceDelete(java.io.File file)
Delete a file. |
static void |
forceDelete(java.lang.String file)
Delete a file. |
static void |
forceDeleteOnExit(java.io.File file)
Schedule a file to be deleted when JVM exits. |
static void |
forceMkdir(java.io.File file)
Make a directory. |
static java.lang.String[] |
getDefaultExcludes()
|
static java.util.List |
getDefaultExcludesAsList()
|
static java.util.List |
getDirectoryNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir)
Return a list of directories as String depending options. |
static java.util.List |
getDirectoryNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir,
boolean isCaseSensitive)
Return a list of directories as String depending options. |
static java.lang.String |
getExtension(java.lang.String filename)
Get extension from filename. |
static java.io.File |
getFile(java.lang.String fileName)
Creates a file handle. |
static java.util.List |
getFileAndDirectoryNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir,
boolean isCaseSensitive,
boolean getFiles,
boolean getDirectories)
Return a list of files as String depending options. |
static java.util.List |
getFileNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir)
Return a list of files as String depending options. |
static java.util.List |
getFileNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir,
boolean isCaseSensitive)
Return a list of files as String depending options. |
static java.util.List |
getFiles(java.io.File directory,
java.lang.String includes,
java.lang.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 java.util.List |
getFiles(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir)
Return the files contained in the directory, using inclusion and exclusion Ant patterns |
static java.lang.String[] |
getFilesFromExtension(java.lang.String directory,
java.lang.String[] extensions)
Given a directory and an array of extensions return an array of compliant files. |
static java.lang.String |
getPath(java.lang.String filepath)
Get path from filename. |
static java.lang.String |
getPath(java.lang.String filepath,
char fileSeparatorChar)
Get path from filename. |
static java.util.List |
loadFile(java.io.File file)
|
static void |
mkdir(java.lang.String dir)
Simple way to make a directory |
static java.lang.String |
normalize(java.lang.String path)
Normalize a path. |
static java.lang.String |
removeExtension(java.lang.String filename)
Remove extension from filename. |
static java.lang.String |
removePath(java.lang.String filepath)
Remove path from filename. |
static java.lang.String |
removePath(java.lang.String filepath,
char fileSeparatorChar)
Remove path from filename. |
static void |
rename(java.io.File from,
java.io.File to)
Renames a file, even if that involves crossing file system boundaries. |
static java.io.File |
resolveFile(java.io.File baseFile,
java.lang.String filename)
Resolve a file filename to it's canonical form. |
static long |
sizeOfDirectory(java.io.File directory)
Recursively count size of a directory. |
static long |
sizeOfDirectory(java.lang.String directory)
Recursively count size of a directory. |
static java.io.File |
toFile(java.net.URL url)
Convert from a URL to a File . |
static java.net.URL[] |
toURLs(java.io.File[] files)
Convert the array of Files into a list of URLs. |
static boolean |
waitFor(java.io.File file,
int seconds)
|
static boolean |
waitFor(java.lang.String fileName,
int seconds)
Waits for NFS to propagate a file creation, imposing a timeout. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int ONE_KB
public static final int ONE_MB
public static final int ONE_GB
public static java.lang.String FS
Constructor Detail |
public FileUtils()
Method Detail |
public static java.lang.String[] getDefaultExcludes()
public static java.util.List getDefaultExcludesAsList()
public static java.lang.String byteCountToDisplaySize(int size)
size
- The number of bytes.
public static java.lang.String dirname(java.lang.String filename)
public static java.lang.String filename(java.lang.String filename)
public static java.lang.String basename(java.lang.String filename)
public static java.lang.String basename(java.lang.String filename, java.lang.String suffix)
public static java.lang.String extension(java.lang.String filename)
public static boolean fileExists(java.lang.String fileName)
fileName
- The name of the file to check.
public static java.lang.String fileRead(java.lang.String file) throws java.io.IOException
java.io.IOException
public static java.lang.String fileRead(java.lang.String file, java.lang.String encoding) throws java.io.IOException
java.io.IOException
public static java.lang.String fileRead(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.lang.String fileRead(java.io.File file, java.lang.String encoding) throws java.io.IOException
java.io.IOException
public static void fileAppend(java.lang.String fileName, java.lang.String data) throws java.io.IOException
fileName
- The name of the file to write.data
- The content to write to the file.
java.io.IOException
public static void fileAppend(java.lang.String fileName, java.lang.String encoding, java.lang.String data) throws java.io.IOException
fileName
- The name of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.
java.io.IOException
public static void fileWrite(java.lang.String fileName, java.lang.String data) throws java.io.IOException
fileName
- The name of the file to write.data
- The content to write to the file.
java.io.IOException
public static void fileWrite(java.lang.String fileName, java.lang.String encoding, java.lang.String data) throws java.io.IOException
fileName
- The name of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.
java.io.IOException
public static void fileDelete(java.lang.String fileName)
fileName
- The name of the file to delete.public static boolean waitFor(java.lang.String fileName, int seconds)
fileName
- The name of the file.seconds
- The maximum time in seconds to wait.
public static boolean waitFor(java.io.File file, int seconds)
public static java.io.File getFile(java.lang.String fileName)
fileName
- The name of the file.
File
manager.public static java.lang.String[] getFilesFromExtension(java.lang.String directory, java.lang.String[] extensions)
public static void mkdir(java.lang.String dir)
public static boolean contentEquals(java.io.File file1, java.io.File file2) throws java.io.IOException
file1
- the first filefile2
- the second file
java.io.IOException
public static java.io.File toFile(java.net.URL url)
URL
to a File
.
url
- File URL.
File
object, or null
if the URL's protocol
is not file
public static java.net.URL[] toURLs(java.io.File[] files) throws java.io.IOException
files
- the array of files
java.io.IOException
- if an error occurspublic static java.lang.String removeExtension(java.lang.String filename)
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename
- the filename
public static java.lang.String getExtension(java.lang.String filename)
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
filename
- the filename
public static java.lang.String removePath(java.lang.String filepath)
basename
ie.
a/b/c.txt --> c.txt a.txt --> a.txt
filepath
- the filepath
public static java.lang.String removePath(java.lang.String filepath, char fileSeparatorChar)
a/b/c.txt --> c.txt a.txt --> a.txt
filepath
- the filepath
public static java.lang.String getPath(java.lang.String filepath)
dirname
.
ie.
a/b/c.txt --> a/b a.txt --> ""
filepath
- the filepath
public static java.lang.String getPath(java.lang.String filepath, char fileSeparatorChar)
a/b/c.txt --> a/b a.txt --> ""
filepath
- the filepath
public static void copyFileToDirectory(java.lang.String source, java.lang.String destinationDirectory) throws java.io.IOException
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
java.io.FileNotFoundException
- if source
isn't a normal file.
java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.
java.io.IOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error occurs during copying.public static void copyFileToDirectoryIfModified(java.lang.String source, java.lang.String destinationDirectory) throws java.io.IOException
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
java.io.FileNotFoundException
- if source
isn't a normal file.
java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.
java.io.IOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error occurs during copying.public static void copyFileToDirectory(java.io.File source, java.io.File destinationDirectory) throws java.io.IOException
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
java.io.FileNotFoundException
- if source
isn't a normal file.
java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.
java.io.IOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error occurs during copying.public static void copyFileToDirectoryIfModified(java.io.File source, java.io.File destinationDirectory) throws java.io.IOException
destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
java.io.FileNotFoundException
- if source
isn't a normal file.
java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.
java.io.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(java.io.File source, java.io.File destination) throws java.io.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).
java.io.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 copyFileToDirectory(java.lang.String, java.lang.String)
).public static boolean copyFileIfModified(java.io.File source, java.io.File destination) throws java.io.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).
java.io.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 copyFileToDirectory(java.lang.String, java.lang.String)
).public static void copyURLToFile(java.net.URL source, java.io.File destination) throws java.io.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).
java.io.IOException
- if
source
URL cannot be openeddestination
cannot be written topublic static java.lang.String normalize(java.lang.String path)
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
path
- the path to normalize
null
if too many ..'s.public static java.lang.String catPath(java.lang.String lookupPath, java.lang.String path)
..
will be
properly handled.
Eg.,
/a/b/c
+ d
= /a/b/d
/a/b/c
+ ../d
= /a/d
public static java.io.File resolveFile(java.io.File baseFile, java.lang.String filename)
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.
baseFile
- Where to resolve filename
from, if filename
is
relative.filename
- Absolute or relative file path to resolve.
File
of filename
.public static void forceDelete(java.lang.String file) throws java.io.IOException
java.io.IOException
public static void forceDelete(java.io.File file) throws java.io.IOException
java.io.IOException
public static void forceDeleteOnExit(java.io.File file) throws java.io.IOException
java.io.IOException
public static void forceMkdir(java.io.File file) throws java.io.IOException
java.io.IOException
public static void deleteDirectory(java.lang.String directory) throws java.io.IOException
java.io.IOException
public static void deleteDirectory(java.io.File directory) throws java.io.IOException
java.io.IOException
public static void cleanDirectory(java.lang.String directory) throws java.io.IOException
java.io.IOException
public static void cleanDirectory(java.io.File directory) throws java.io.IOException
java.io.IOException
public static long sizeOfDirectory(java.lang.String directory)
public static long sizeOfDirectory(java.io.File directory)
public static java.util.List getFiles(java.io.File directory, java.lang.String includes, java.lang.String excludes) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separated
java.io.IOException
getFileNames( File, String, String, boolean )
public static java.util.List getFiles(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each file
java.io.IOException
getFileNames( File, String, String, boolean )
public static java.util.List getFileNames(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of file
java.io.IOException
public static java.util.List getFileNames(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitive
java.io.IOException
public static java.util.List getDirectoryNames(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of file
java.io.IOException
public static java.util.List getDirectoryNames(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitive
java.io.IOException
public static java.util.List getFileAndDirectoryNames(java.io.File directory, java.lang.String includes, java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories) throws java.io.IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitivegetFiles
- true if get filesgetDirectories
- true if get directories
java.io.IOException
public static void copyDirectory(java.io.File sourceDirectory, java.io.File destinationDirectory) throws java.io.IOException
java.io.IOException
public static void copyDirectory(java.io.File sourceDirectory, java.io.File destinationDirectory, java.lang.String includes, java.lang.String excludes) throws java.io.IOException
java.io.IOException
public static void copyDirectoryStructure(java.io.File sourceDirectory, java.io.File destinationDirectory) throws java.io.IOException
sourceDirectory
must exists.
sourceDirectory
- destinationDirectory
-
java.io.IOException
public static void copyDirectoryStructureIfModified(java.io.File sourceDirectory, java.io.File destinationDirectory) throws java.io.IOException
sourceDirectory
must exists.
sourceDirectory
- destinationDirectory
-
java.io.IOException
public static void rename(java.io.File from, java.io.File to) throws java.io.IOException
This will remove to
(if it exists), ensure that
to
's parent directory exists and move
from
, which involves deleting from
as
well.
from
- the file to moveto
- the new file name
java.io.IOException
- if anything bad happens during this
process. Note that to
may have been deleted
already when this happens.public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File parentDir)
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 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.
prefix
- prefix before the random numbersuffix
- file extension; include the '.'parentDir
- Directory to create the temporary file in -
java.io.tmpdir used if not specificed
public static void copyFile(java.io.File from, java.io.File to, java.lang.String encoding, FileUtils.FilterWrapper[] wrappers) throws java.io.IOException
java.io.IOException
public static java.util.List loadFile(java.io.File file) throws java.io.IOException
java.io.IOException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |