|
||||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.spice.salt.io.FileUtil
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.
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.
Method Summary | |
---|---|
static String |
catPath(String lookupPath,
String path)
Will concatenate 2 paths. |
static void |
cleanDirectory(File directory)
Clean a directory without deleting it. |
static void |
cleanDirectory(String 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 |
copyFile(File source,
File destination)
Copy file from source to destination. |
static void |
copyFileToDirectory(File source,
File destinationDirectory)
Copy file from source to destination. |
static void |
copyFileToDirectory(String source,
String 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 |
deleteDirectory(String directory)
Recursively delete a directory. |
static void |
forceDelete(File file)
Delete a file. |
static void |
forceDelete(String 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 |
getExtension(String filename)
Get extension from filename. |
static String |
getPath(String filepath)
Get path from filename. |
static String |
getPath(String filepath,
char fileSeparatorChar)
Get path from filename. |
static String |
normalize(String path)
Normalize a path. |
static String |
removeExtension(String filename)
Remove extension from filename. |
static String |
removePath(String filepath)
Remove path from filename. |
static String |
removePath(String filepath,
char fileSeparatorChar)
Remove path from filename. |
static File |
resolveFile(File baseFile,
String filename)
Resolve a file filename to it's canonical form. |
static File[] |
resolveFileSet(File base,
PathMatcher matcher)
Resolve a fileset in a particular hierarchy. |
static long |
sizeOfDirectory(File directory)
Recursively count size of a directory. |
static long |
sizeOfDirectory(String 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static boolean contentEquals(File file1, File file2) throws IOException
file1
- the first filefile2
- the second file
IOException
public 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 String removeExtension(String filename)
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename
- the filename
public static String getExtension(String filename)
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
filename
- the filename
public static String removePath(String filepath)
basename
ie.
a/b/c.txt --> c.txt a.txt --> a.txt
filepath
- the filepath
public static String removePath(String filepath, char fileSeparatorChar)
a/b/c.txt --> c.txt a.txt --> a.txt
filepath
- the filepath
public static String getPath(String filepath)
dirname
. ie.
a/b/c.txt --> a/b a.txt --> ""
filepath
- the filepath
public static String getPath(String filepath, char fileSeparatorChar)
a/b/c.txt --> a/b a.txt --> ""
filepath
- the filepath
public static void copyFileToDirectory(String source, String 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.
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 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.
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.
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.lang.String, java.lang.String)
).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 final String normalize(String path)
Eg:
/foo// --> /foo/ /foo/./ --> /foo/ /foo/../bar --> /bar /foo/../bar/ --> /bar/ /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null
path
- the path to be normalized.
NullPointerException
- if path is null.public static String catPath(String lookupPath, String path)
..
will be properly
handled. Eg.,
/a/b/c
+ d
=
/a/b/d
/a/b/c
+ ../d
=
/a/d
Note: this method handles java/unix style path only (separator is '/').
normalize).
NullPointerException
- if any parameter is null.normalize(java.lang.String)
public static File resolveFile(File baseFile, 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(String file) throws IOException
IOException
public static void forceDelete(File file) throws IOException
IOException
public static void forceDeleteOnExit(File file) throws IOException
IOException
public static void forceMkdir(File file) throws IOException
IOException
public static void deleteDirectory(String directory) throws IOException
IOException
public static void deleteDirectory(File directory) throws IOException
IOException
public static void cleanDirectory(String directory) throws IOException
IOException
public static void cleanDirectory(File directory) throws IOException
IOException
public static long sizeOfDirectory(String directory)
public static long sizeOfDirectory(File directory)
public static final File[] resolveFileSet(File base, PathMatcher matcher)
base
- the file hierarchy to usematcher
- the matcher to use while scanning
|
||||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |