|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
@ComponentSpecification public interface FileUtil
This is the interface for a collection of utility functions for File
handling and manipulation.
FileUtilImpl| Field Summary | |
|---|---|
static File[] |
NO_FILES
An empty file array. |
static String |
PATH_SEGMENT_CURRENT
The path segment indicating the current folder itself. |
static String |
PATH_SEGMENT_PARENT
The path segment indicating the parent folder. |
static String |
PROPERTY_TMP_DIR
The key of the system property
"java.io.tmpdir". |
static String |
PROPERTY_USER_HOME
The key of the system property
"user.home". |
| Method Summary | |
|---|---|
boolean |
collectMatchingFiles(File cwd,
String path,
FileType fileType,
List<File> list)
This method adds all files matching the given path and
fileType to the list. |
void |
copyFile(File source,
File destination)
This method copies the file given by source to the file given
by destination. |
void |
copyFile(File source,
File destination,
boolean keepFlags)
This method copies the file given by source to the file given
by destination. |
void |
copyRecursive(File source,
File destination,
boolean allowOverwrite)
This method copies the file or directory given by source into
the given destination. |
void |
copyRecursive(File source,
File destination,
boolean allowOverwrite,
FileFilter filter)
This method copies the file or directory given by source into
the given destination. |
int |
deleteChildren(File directory)
This method deletes all children of the given directory recursively. |
int |
deleteRecursive(File path)
This method deletes the given path. |
String |
getBasename(String filename)
This method gets the basename of the given filename
(path). |
String |
getDirname(String filename)
This method gets the directory-name of the given filename
(path). |
String |
getExtension(String filename)
This method extracts the extension from the given filename. |
File[] |
getMatchingFiles(File cwd,
String path,
FileType fileType)
This method gets all files matching to the given
path and fileType. |
FileAccessPermissions |
getPermissions(File file,
FileAccessClass accessClass)
This method gets the permissions of the given
file. |
File |
getTemporaryDirectory()
This method gets the File representing the
temporary directory. |
File |
getUserHomeDirectory()
This method gets the File representing the
home directory of the user. |
String |
normalizePath(String path)
This method is a shortcut for . |
String |
normalizePath(String path,
char separator)
This method normalizes a given path. |
void |
setPermissions(File file,
FileAccessPermissions permissions)
This method sets the
of the given file. |
| Field Detail |
|---|
static final String PATH_SEGMENT_CURRENT
static final String PATH_SEGMENT_PARENT
static final String PROPERTY_USER_HOME
system property
"user.home". It contains the home directory of the user that
started this JVM./home/mylogin or
C:\Windows\Profiles\mylogin.
static final String PROPERTY_TMP_DIR
system property
"java.io.tmpdir". It contains the directory to use for temporary
files./tmp, C:\Temp or
/usr/local/tomcat/temp.
static final File[] NO_FILES
| Method Detail |
|---|
File getUserHomeDirectory()
File representing the
home directory of the user.
File getTemporaryDirectory()
File representing the
temporary directory.
String normalizePath(String path,
char separator)
path. It will resolve ".." and
"." segments, normalize backslashes and remove duplicated slashes. Further
it can resolve "~" at the beginning of the path (like in bash-scripts,
etc.). Therefore this method resolves the path in such situations (e.g. to
"/home/login/foo") and returns a physical path.separator is '/'
(backslashes are NOT escaped):
path |
normalizePath(path) |
|---|---|
"folder/subfolder//../.\some.file" |
"folder/some.file" |
"../.\some.file" |
"../some.file" |
"http://www.host.com/foo/bar/./test/.././.." |
"http://www.host.com/foo" |
"\\unc.host\printers\pr3761" |
"\\unc.host\printers\pr3761" |
"~/documents/index.html" |
|
"~root/subfolder/../folder/.//index.html" |
"/root/folder/index.html" |
path - is the path to resolve.separator - is the character to use as file
separator.
String normalizePath(String path)
normalizePath(path,
File.separatorChar).
path - is the path to resolve.
normalizePath(String, char)String getExtension(String filename)
filename.getExtension("test.java") would
return "java".filename is just a dot followed by the extension (e.g.
".java"), the empty string is returned.
filename - is the filename and may include an absolute or relative
path.
filename excluding the dot
in lowercase or the empty string if
NOT present.String getBasename(String filename)
filename
(path). The basename is the raw name of the file without the
path.| filename | |
|---|---|
| / | / |
| \/\ | \ |
| /. | . |
| /foo.bar | foo.bar |
| /foo/bar/ | bar |
| c:\\ | |
| c:\\foo | foo |
| http://foo.org/bar | bar |
filename - is the path to a file or directory.
filename.String getDirname(String filename)
filename
(path).| filename | getDirname(String) |
|---|---|
| foo | . |
| /foo | / |
| /foo/bar | /foo |
| /foo/bar/ | /foo |
| ./foo/bar/ | ./foo |
| ./foo/bar/../ | ./foo/bar |
filename - is the path to a file or directory.
filename.normalizePath(String)
void copyFile(File source,
File destination)
throws RuntimeIoException
source to the file given
by destination.
source - is the existing file to copy from.destination - is the file to copy to. It will be created if it does
NOT exist and overridden otherwise.
RuntimeIoException - if the operation fails.
void copyFile(File source,
File destination,
boolean keepFlags)
throws RuntimeIoException
source to the file given
by destination.
source - is the existing file to copy from.destination - is the file to copy to. It will be created if it does
NOT exist and overridden otherwise.keepFlags - - true if the flags of the file should be
copied as well, false otherwise (a new file is created
with default flags and only the content is copied).
RuntimeIoException - if the operation fails.
FileAccessPermissions getPermissions(File file,
FileAccessClass accessClass)
permissions of the given
file.
file - is the file for which the permissions are requested.accessClass - is the distinct class the
permission should be applied to in the returned permissions. It may
be null to apply the permissions to all distinct
classes.
file.
void setPermissions(File file,
FileAccessPermissions permissions)
permissions
of the given file.FileAccessClass.OTHERS and FileAccessClass.USER
so FileAccessClass.GROUP flags are ignored as well as the global
s-bits (sticky,
setgid and
setuid).
file - is the file to modify.permissions - are the permissions to set.
void copyRecursive(File source,
File destination,
boolean allowOverwrite)
throws RuntimeIoException
source into
the given destination.source a new
name, the destination has to point to
the final place where the copy should appear rather than the directory
where the copy will be located in.Filesource = newFile("/usr/local/foo");Filedestination = newFile("/tmp", source.getName()); // file: "/tmp/foo"fileUtil.copyRecursive(source, destination, true);
source - is the file or directory to copy.destination - is the final place where the copy should appear.allowOverwrite - - if false and the
destination already exists, a
RuntimeIoException is thrown, else if true the
destination will be overwritten.
RuntimeIoException - if the operation fails.
void copyRecursive(File source,
File destination,
boolean allowOverwrite,
FileFilter filter)
throws RuntimeIoException
source into
the given destination.source a new
name, the destination has to point to
the final place where the copy should appear rather than the directory
where the copy will be located in.
source - is the file or directory to copy.destination - is the final place where the copy should appear.allowOverwrite - - if false and the
destination already exists, a
RuntimeIoException is thrown, else if true the
destination will be overwritten.filter - is a FileFilter that decides which files should be copied. Only
accepted files and directories are
copied, others will be ignored.
RuntimeIoException - if the operation fails.copyRecursive(File, File, boolean)
int deleteRecursive(File path)
throws RuntimeIoException
deletes the given path. If
the path denotes a directory then
it will be deleted recursively.
path - is the path to delete.
RuntimeIoException - if a file or directory could NOT be
deleted.deleteChildren(File)
int deleteChildren(File directory)
throws RuntimeIoException
deletes all children of the given directory recursively. If the given
directory denotes an existing
directory then it will be empty after the call
of this method, else this method will have no effect.
directory - is the directory to delete.
RuntimeIoException - if a file or directory could NOT be
deleted.
File[] getMatchingFiles(File cwd,
String path,
FileType fileType)
files matching to the given
path and fileType. The path may
contain wildcards.getMatchingFiles(cwd,
"*", FileType.DIRECTORY) will return all
directories in cwdgetMatchingFiles(cwd,
"*/*.xml", FileType.FILE) will return all
files from all direct sub-folders
of cwd that end with ".xml"getMatchingFiles(cwd,
"**/*.xml", FileType.FILE) will return all
files in cwd or any of its transitive
sub-folders that end with ".xml"
cwd - is the current working directory and should therefore point to
an existing directory. If the given
path is NOT absolute it is
interpreted relative to this directory.path - is the path the requested files must match. If this path is NOT
absolute it is interpreted relative to the
directory given by cwd.fileType - is the type of the requested files or null if
files of any type are acceptable.
files that match the given
path and apply to ignorecollectMatchingFiles(File, String, FileType, List)
boolean collectMatchingFiles(File cwd,
String path,
FileType fileType,
List<File> list)
path and
fileType to the list. The path may
contain wildcards.
cwd - is the current working directory and should therefore point to
an existing directory. If the given
path is NOT absolute it is
interpreted relative to this directory.path - is the path the files to collect must match. If this path is
NOT absolute it is interpreted relative to
the directory given by cwd.fileType - is the type of the files to collect or null if
files of any type are acceptable.list - is the list where to add the collected
files.
false if the path is a regular string and
true if the given path contains at least one
wildcard (
'*' or '?').
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||