public class FileUtilImpl extends FileUtilLimitedImpl implements FileUtil
File
handling and manipulation.getInstance()
Modifier and Type | Field and Description |
---|---|
private static FileUtil |
instance |
private StringUtil |
stringUtil |
private File |
temporaryDirectory |
private String |
temporaryDirectoryPath |
private File |
userHomeDirectory |
private String |
userHomeDirectoryPath |
private String |
userLogin |
HOME_ROOT
NO_FILES, PATH_SEGMENT_CURRENT, PATH_SEGMENT_PARENT, PROPERTY_TMP_DIR, PROPERTY_USER_HOME
Constructor and Description |
---|
FileUtilImpl()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
private void |
collectMatchingFiles(File cwd,
List<ResourcePathNode<Pattern>> segments,
int segmentIndex,
FileType fileType,
List<File> list)
This method adds all files matching with the given
path and fileType to the list . |
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 . |
private void |
copyRecursive(File source,
File destination,
FileFilter filter)
This method copies the file or directory given by
source into the given destination . |
int |
deleteChildren(File directory)
|
int |
deleteRecursive(File path)
This method
deletes the given path . |
protected void |
doInitialize()
This method performs the actual
initialization . |
static FileUtil |
getInstance()
This method gets the singleton instance of this
FileUtilImpl . |
File[] |
getMatchingFiles(File cwd,
String path,
FileType fileType)
|
FileAccessPermissions |
getPermissions(File file,
FileAccessClass accessClass)
This method gets the
permissions of the given file . |
protected StringUtil |
getStringUtil()
This method gets the
StringUtilImpl that is used by this FileUtilImpl . |
File |
getTemporaryDirectory()
This method gets the
File representing the temporary directory . |
File |
getUserHomeDirectory()
This method gets the
File representing the home directory of the
current user . |
protected String |
getUserHomeDirectoryPath() |
String |
getUserLogin() |
void |
setPermissions(File file,
FileAccessPermissions permissions)
This method sets the
permissions of the given file . |
void |
setStringUtil(StringUtil stringUtil)
This method sets the
StringUtil . |
void |
setTemporaryDirectoryPath(String tmpDir)
This method sets the
tmp directory . |
void |
setUserHomeDirectoryPath(String userHome)
This method set the
users home directory . |
void |
setUserLogin(String userLogin) |
getBasename, getDirname, getExtension, normalizeHome, normalizePath, normalizePath
createLogger, getLogger
doInitialized, getInitializationState, initialize
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getBasename, getDirname, getExtension, normalizePath, normalizePath
private static FileUtil instance
private StringUtil stringUtil
private String userHomeDirectoryPath
private File userHomeDirectory
private String userLogin
private String temporaryDirectoryPath
private File temporaryDirectory
public static FileUtil getInstance()
FileUtilImpl
. Cdi.GET_INSTANCE
before using.protected void doInitialize()
AbstractComponent
initialization
. It is called when AbstractComponent.initialize()
is
invoked for the first time. super.
AbstractComponent.doInitialize()
.doInitialize
in class AbstractLoggableComponent
protected StringUtil getStringUtil()
StringUtilImpl
that is used by this FileUtilImpl
.StringUtilImpl
.@Inject public void setStringUtil(StringUtil stringUtil) throws AlreadyInitializedException
StringUtil
. It can only be set once during initialization.stringUtil
- the stringUtil to set.AlreadyInitializedException
- if the value has already been set.public File getUserHomeDirectory()
FileUtil
File
representing the home directory of the
current user
.getUserHomeDirectory
in interface FileUtil
protected String getUserHomeDirectoryPath()
getUserHomeDirectoryPath
in class FileUtilLimitedImpl
public void setUserHomeDirectoryPath(String userHome) throws AlreadyInitializedException
users home directory
. It can only be set during
initialization.userHome
- is the home directory of the user.AlreadyInitializedException
- if the value has already been set.public File getTemporaryDirectory()
FileUtil
File
representing the temporary directory
.getTemporaryDirectory
in interface FileUtil
public void setTemporaryDirectoryPath(String tmpDir) throws AlreadyInitializedException
tmp directory
.tmpDir
- the tmpDir to setAlreadyInitializedException
- if the value has already been set.public String getUserLogin()
getUserLogin
in class FileUtilLimitedImpl
public void setUserLogin(String userLogin)
userLogin
- is the userLogin to setpublic void copyFile(File source, File destination)
FileUtil
source
to the file given by destination
.public void copyFile(File source, File destination, boolean keepFlags)
FileUtil
source
to the file given by destination
. copyFile
in interface FileUtil
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).public void copyRecursive(File source, File destination, boolean allowOverwrite)
FileUtil
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. File
source = newFile
("/usr/local/foo");File
destination = newFile
("/tmp", source.getName()); // file: "/tmp/foo"fileUtil
.copyRecursive
(source, destination, true);
copyRecursive
in interface FileUtil
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.public void copyRecursive(File source, File destination, boolean allowOverwrite, FileFilter filter)
FileUtil
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. copyRecursive
in interface FileUtil
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.FileUtil.copyRecursive(File, File, boolean)
private void copyRecursive(File source, File destination, FileFilter filter)
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. File
source = newFile
("/usr/local/foo");File
destination = newFile
("/tmp", source.getName()); // file: "/tmp/foo"FileUtilImpl
.copyRecursive(source, destination, true);
source
- is the file or directory to copy.destination
- is the final place where the copy should appear.filter
- is a FileFilter
that decides
which files should be
copied. Only accepted
files and directories are copied, others
will be ignored.public FileAccessPermissions getPermissions(File file, FileAccessClass accessClass)
FileUtil
permissions
of the given file
. getPermissions
in interface FileUtil
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
.public void setPermissions(File file, FileAccessPermissions permissions)
FileUtil
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
).setPermissions
in interface FileUtil
file
- is the file to modify.permissions
- are the permissions to set.public int deleteRecursive(File path)
FileUtil
deletes
the given path
. If the path
denotes a
directory
then it will be deleted recursively.deleteRecursive
in interface FileUtil
path
- is the path to delete.FileUtil.deleteChildren(File)
public int deleteChildren(File directory)
FileUtil
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.deleteChildren
in interface FileUtil
directory
- is the directory to delete.public File[] getMatchingFiles(File cwd, String path, FileType fileType)
FileUtil
files
matching to the given path
and fileType
. The
path
may contain wildcards
. getMatchingFiles
(cwd,
"*", FileType.DIRECTORY
)
will return all directories
in
cwd
getMatchingFiles
(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"getMatchingFiles
in interface FileUtil
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
ignore
FileUtil.collectMatchingFiles(File, String, FileType, List)
public boolean collectMatchingFiles(File cwd, String path, FileType fileType, List<File> list)
FileUtil
path
and fileType
to the list
. The
path
may contain wildcards
.collectMatchingFiles
in interface FileUtil
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
'?'
).private void collectMatchingFiles(File cwd, List<ResourcePathNode<Pattern>> segments, int segmentIndex, FileType fileType, List<File> list)
path
and fileType
to the list
.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.segments
- 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
.segmentIndex
- is the current index in pathChars
for the collection process.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.Copyright © 2001–2016 mmm-Team. All rights reserved.