Class FileUtils
- java.lang.Object
-
- org.codehaus.plexus.util.FileUtils
-
public class FileUtils extends Object
This class provides basic facilities for manipulating files and file paths.
Path-related methodsMethods 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 throughgetPath(java.lang.String)
index.html
-- retrievable throughremovePath(java.lang.String)
/www/hosted/mysite/index
-- retrievable throughremoveExtension(java.lang.String)
html
-- retrievable throughgetExtension(java.lang.String)
There are also methods to
File-related methodsconcatenate two paths
,resolve a path relative to a File
andnormalize(java.lang.String)
a path.There are methods to create a
File from a URL
, copy aFile to a directory
, copy aFile to another File
, copy aURL's contents to a File
, as well as methods todelete
andclean
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
Nested Classes Modifier and Type Class Description static class
FileUtils.FilterWrapper
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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(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
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 directoriesstatic 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 ifto.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 ifto.lastModified() < from.lastModified()
, if overwrite is truestatic 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(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
copyFileToDirectoryIfModified(File source, File destinationDirectory)
Copy file from source to destination only if source is newer than the target file.static void
copyFileToDirectoryIfModified(String source, String destinationDirectory)
Copy file from source to destination only if source is newer than the target file.static void
copyStreamToFile(InputStreamFacade source, File destination)
static void
copyURLToFile(URL source, File destination)
Copies bytes from the URLsource
to a filedestination
.static File
createTempFile(String prefix, String suffix, File parentDir)
Create a temporary file in a given directory.static void
deleteDirectory(File directory)
Recursively delete a directory.static void
deleteDirectory(String 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(File file)
Note: the file content is read with platform encodingstatic String
fileRead(File file, String encoding)
static String
fileRead(String file)
Note: the file content is read with platform encoding.static String
fileRead(String file, String encoding)
static void
fileWrite(File file, String data)
Writes data to a file.static void
fileWrite(File file, String encoding, String data)
Writes data to a file.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(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[]
getDefaultExcludes()
static List<String>
getDefaultExcludesAsList()
static String
getDefaultExcludesAsString()
static List<String>
getDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir)
Return a list of directories as String depending options.static List<String>
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. iestatic File
getFile(String fileName)
Creates a file handle.static List<String>
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<String>
getFileNames(File directory, String includes, String excludes, boolean includeBasedir)
Return a list of files as String depending options.static List<String>
getFileNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive)
Return a list of files as String depending options.static List<File>
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 filesstatic List<File>
getFiles(File directory, String includes, String excludes, boolean includeBasedir)
Return the files contained in the directory, using inclusion and exclusion Ant patternsstatic 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 void
linkFile(File source, File destination)
Link file from destination to source.static List<String>
loadFile(File file)
Note: the file content is read with platform encodingstatic void
mkdir(String dir)
Simple way to make a directorystatic void
mkDirs(File sourceBase, String[] dirs, File destination)
Creates a number of directories, as delivered from DirectoryScannerstatic String
normalize(String path)
Normalize a path.static String
removeExtension(String filename)
Remove extension from filename. iestatic 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 filefilename
to it's canonical form.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 aURL
to aFile
.static URL[]
toURLs(File[] files)
Convert the array of Files into a list of URLs.static boolean
waitFor(File file, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.static boolean
waitFor(String fileName, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
-
-
-
Field Detail
-
ONE_KB
public static final int ONE_KB
The number of bytes in a kilobyte.- See Also:
- Constant Field Values
-
ONE_MB
public static final int ONE_MB
The number of bytes in a megabyte.- See Also:
- Constant Field Values
-
ONE_GB
public static final int ONE_GB
The number of bytes in a gigabyte.- See Also:
- Constant Field Values
-
FS
public static String FS
The vm file separator
-
-
Method Detail
-
getDefaultExcludes
public static String[] getDefaultExcludes()
- Returns:
- the default excludes pattern
- See Also:
AbstractScanner.DEFAULTEXCLUDES
-
getDefaultExcludesAsList
public static List<String> getDefaultExcludesAsList()
- Returns:
- the default excludes pattern as list.
- See Also:
getDefaultExcludes()
-
getDefaultExcludesAsString
public static String getDefaultExcludesAsString()
- Returns:
- the default excludes pattern as comma separated string.
- See Also:
AbstractScanner.DEFAULTEXCLUDES
,StringUtils.join(Object[], String)
-
byteCountToDisplaySize
public static String byteCountToDisplaySize(int size)
Returns a human-readable version of the file size (original is in bytes).- Parameters:
size
- The number of bytes.- Returns:
- A human-readable display value (includes units).
-
dirname
public static String dirname(String filename)
Returns the directory path portion of a file specification string. Matches the equally named unix command.- Parameters:
filename
- the file path- Returns:
- The directory portion excluding the ending file separator.
-
filename
public static String filename(String filename)
Returns the filename portion of a file specification string.- Parameters:
filename
- the file path- Returns:
- The filename string with extension.
-
basename
public static String basename(String filename)
Returns the filename portion of a file specification string. Matches the equally named unix command.- Parameters:
filename
- the file path- Returns:
- The filename string without extension.
-
basename
public static String basename(String filename, String suffix)
Returns the filename portion of a file specification string. Matches the equally named unix command.- Parameters:
filename
- the file pathsuffix
- the file suffix- Returns:
- the basename of the file
-
extension
public static String extension(String filename)
Returns the extension portion of a file specification string. This everything after the last dot '.' in the filename (NOT including the dot).- Parameters:
filename
- the file path- Returns:
- the extension of the file
-
fileExists
public static boolean fileExists(String fileName)
Check if a file exits.- Parameters:
fileName
- the file path.- Returns:
- true if file exists.
-
fileRead
public static String fileRead(String file) throws IOException
Note: the file content is read with platform encoding.- Parameters:
file
- the file path- Returns:
- the file content using the platform encoding.
- Throws:
IOException
- if any
-
fileRead
public static String fileRead(String file, String encoding) throws IOException
- Parameters:
file
- the file pathencoding
- the wanted encoding- Returns:
- the file content using the specified encoding.
- Throws:
IOException
- if any
-
fileRead
public static String fileRead(File file) throws IOException
Note: the file content is read with platform encoding- Parameters:
file
- the file path- Returns:
- the file content using the platform encoding.
- Throws:
IOException
- if any
-
fileRead
public static String fileRead(File file, String encoding) throws IOException
- Parameters:
file
- the file pathencoding
- the wanted encoding- Returns:
- the file content using the specified encoding.
- Throws:
IOException
- if any
-
fileAppend
public static void fileAppend(String fileName, String data) throws IOException
Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform encoding- Parameters:
fileName
- The path of the file to write.data
- The content to write to the file.- Throws:
IOException
- if any
-
fileAppend
public static void fileAppend(String fileName, String encoding, String data) throws IOException
Appends data to a file. The file will be created if it does not exist.- 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
-
fileWrite
public static void fileWrite(String fileName, String data) throws IOException
Writes data to a file. The file will be created if it does not exist. Note: the data is written with platform encoding- Parameters:
fileName
- The path of the file to write.data
- The content to write to the file.- Throws:
IOException
- if any
-
fileWrite
public static void fileWrite(String fileName, String encoding, String data) throws IOException
Writes data to a file. The file will be created if it does not exist.- 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
-
fileWrite
public static void fileWrite(File file, String data) throws IOException
Writes data to a file. The file will be created if it does not exist. Note: the data is written with platform encoding- Parameters:
file
- The file to write.data
- The content to write to the file.- Throws:
IOException
- if any- Since:
- 2.0.6
-
fileWrite
public static void fileWrite(File file, String encoding, String data) throws IOException
Writes data to a file. The file will be created if it does not exist.- Parameters:
file
- The file to write.encoding
- The encoding of the file.data
- The content to write to the file.- Throws:
IOException
- if any- Since:
- 2.0.6
-
fileDelete
public static void fileDelete(String fileName)
Deletes a file.- Parameters:
fileName
- The path of the file to delete.
-
waitFor
public static boolean waitFor(String fileName, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.- Parameters:
fileName
- The path of the file.seconds
- The maximum time in seconds to wait.- Returns:
- True if file exists.
-
waitFor
public static boolean waitFor(File file, int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.- Parameters:
file
- The file.seconds
- The maximum time in seconds to wait.- Returns:
- True if file exists.
-
getFile
public static File getFile(String fileName)
Creates a file handle.- Parameters:
fileName
- The path of the file.- Returns:
- A
File
manager.
-
getFilesFromExtension
public static String[] getFilesFromExtension(String directory, String[] extensions)
Given a directory and an array of extensions return an array of compliant files.
TODO Should an ignore list be passed in? TODO Should a recurse flag be passed in?
The given extensions should be like "java" and not like ".java"
- Parameters:
directory
- The path of the directory.extensions
- an array of expected extensions.- Returns:
- An array of files for the wanted extensions.
-
mkdir
public static void mkdir(String dir)
Simple way to make a directory- 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
-
contentEquals
public static boolean contentEquals(File file1, File file2) throws IOException
Compare the contents of two files to determine if they are equal or not.- Parameters:
file1
- the first filefile2
- 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
-
toFile
public static File toFile(URL url)
Convert from aURL
to aFile
.- Parameters:
url
- File URL.- Returns:
- The equivalent
File
object, ornull
if the URL's protocol is notfile
-
toURLs
public static URL[] toURLs(File[] files) throws IOException
Convert the array of Files into a list of URLs.- Parameters:
files
- the array of files- Returns:
- the array of URLs
- Throws:
IOException
- if an error occurs
-
removeExtension
public static String removeExtension(String filename)
Remove extension from filename. iefoo.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
-
getExtension
public static String getExtension(String filename)
Get extension from filename. iefoo.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
-
removePath
public static String removePath(String filepath)
Remove path from filename. Equivalent to the unix commandbasename
ie.a/b/c.txt --> c.txt a.txt --> a.txt
- Parameters:
filepath
- the path of the file- Returns:
- the filename minus path
-
removePath
public static String removePath(String filepath, char fileSeparatorChar)
Remove path from filename. ie.a/b/c.txt --> c.txt a.txt --> a.txt
- Parameters:
filepath
- the path of the filefileSeparatorChar
- the file separator character like / on Unix platforms.- Returns:
- the filename minus path
-
getPath
public static String getPath(String filepath)
Get path from filename. Roughly equivalent to the unix commanddirname
. ie.a/b/c.txt --> a/b a.txt --> ""
- Parameters:
filepath
- the filepath- Returns:
- the filename minus path
-
getPath
public static String getPath(String filepath, char fileSeparatorChar)
Get path from filename. ie.a/b/c.txt --> a/b a.txt --> ""
- Parameters:
filepath
- the filepathfileSeparatorChar
- the file separator character like / on Unix platforms.- Returns:
- the filename minus path
-
copyFileToDirectory
public static void copyFileToDirectory(String source, String destinationDirectory) throws IOException
Copy file from source to destination. IfdestinationDirectory
does not exist, it (and any parent directories) will be created. If a filesource
indestinationDirectory
exists, it will be overwritten.- Parameters:
source
- An existingFile
to copy.destinationDirectory
- A directory to copysource
into.- Throws:
FileNotFoundException
- ifsource
isn't a normal file.IllegalArgumentException
- ifdestinationDirectory
isn't a directory.IOException
- ifsource
does not exist, the file indestinationDirectory
cannot be written to, or an IO error occurs during copying.
-
copyFileToDirectoryIfModified
public static void copyFileToDirectoryIfModified(String source, String destinationDirectory) throws IOException
Copy file from source to destination only if source is newer than the target file. IfdestinationDirectory
does not exist, it (and any parent directories) will be created. If a filesource
indestinationDirectory
exists, it will be overwritten.- Parameters:
source
- An existingFile
to copy.destinationDirectory
- A directory to copysource
into.- Throws:
FileNotFoundException
- ifsource
isn't a normal file.IllegalArgumentException
- ifdestinationDirectory
isn't a directory.IOException
- ifsource
does not exist, the file indestinationDirectory
cannot be written to, or an IO error occurs during copying.
-
copyFileToDirectory
public static void copyFileToDirectory(File source, File destinationDirectory) throws IOException
Copy file from source to destination. IfdestinationDirectory
does not exist, it (and any parent directories) will be created. If a filesource
indestinationDirectory
exists, it will be overwritten.- Parameters:
source
- An existingFile
to copy.destinationDirectory
- A directory to copysource
into.- Throws:
FileNotFoundException
- ifsource
isn't a normal file.IllegalArgumentException
- ifdestinationDirectory
isn't a directory.IOException
- ifsource
does not exist, the file indestinationDirectory
cannot be written to, or an IO error occurs during copying.
-
copyFileToDirectoryIfModified
public static void copyFileToDirectoryIfModified(File source, File destinationDirectory) throws IOException
Copy file from source to destination only if source is newer than the target file. IfdestinationDirectory
does not exist, it (and any parent directories) will be created. If a filesource
indestinationDirectory
exists, it will be overwritten.- Parameters:
source
- An existingFile
to copy.destinationDirectory
- A directory to copysource
into.- Throws:
FileNotFoundException
- ifsource
isn't a normal file.IllegalArgumentException
- ifdestinationDirectory
isn't a directory.IOException
- ifsource
does not exist, the file indestinationDirectory
cannot be written to, or an IO error occurs during copying.
-
mkDirs
public static void mkDirs(File sourceBase, String[] dirs, File destination) throws IOException
Creates a number of directories, as delivered from DirectoryScanner- Parameters:
sourceBase
- The basedir used for the directory scandirs
- The getIncludedDirs from the dirscannerdestination
- The base dir of the output structure- Throws:
IOException
- io issue
-
copyFile
public static void copyFile(File source, File destination) throws IOException
Copy file from source to destination. The directories up todestination
will be created if they don't already exist.destination
will be overwritten if it already exists.- Parameters:
source
- An existing non-directoryFile
to copy bytes from.destination
- A non-directoryFile
to write bytes to (possibly overwriting).- Throws:
IOException
- ifsource
does not exist,destination
cannot be written to, or an IO error occurs during copying.FileNotFoundException
- ifdestination
is a directory (usecopyFileToDirectory(java.lang.String, java.lang.String)
).
-
linkFile
public static void linkFile(File source, File destination) throws IOException
Link file from destination to source. The directories up todestination
will be created if they don't already exist.destination
will be overwritten if it already exists.- Parameters:
source
- An existing non-directoryFile
to link to.destination
- A non-directoryFile
becoming the link (possibly overwriting).- Throws:
IOException
- ifsource
does not exist,destination
cannot be created, or an IO error occurs during linking.FileNotFoundException
- ifdestination
is a directory (usecopyFileToDirectory(java.lang.String, java.lang.String)
).
-
copyFileIfModified
public static boolean copyFileIfModified(File source, File destination) throws IOException
Copy file from source to destination only if source timestamp is later than the destination timestamp. The directories up todestination
will be created if they don't already exist.destination
will be overwritten if it already exists.- Parameters:
source
- An existing non-directoryFile
to copy bytes from.destination
- A non-directoryFile
to write bytes to (possibly overwriting).- Returns:
- true if no problem occured
- Throws:
IOException
- ifsource
does not exist,destination
cannot be written to, or an IO error occurs during copying.
-
copyURLToFile
public static void copyURLToFile(URL source, File destination) throws IOException
Copies bytes from the URLsource
to a filedestination
. The directories up todestination
will be created if they don't already exist.destination
will be overwritten if it already exists.- Parameters:
source
- AURL
to copy bytes from.destination
- A non-directoryFile
to write bytes to (possibly overwriting).- Throws:
IOException
- ifsource
URL cannot be openeddestination
cannot be written to- an IO error occurs during copying
-
copyStreamToFile
public static void copyStreamToFile(InputStreamFacade source, File destination) throws IOException
Copies bytes from theInputStream
source
to a filedestination
. The directories up todestination
will be created if they don't already exist.destination
will be overwritten if it already exists.- Parameters:
source
- AnInputStream
to copy bytes from. This stream is guaranteed to be closed.destination
- A non-directoryFile
to write bytes to (possibly overwriting).- Throws:
IOException
- ifsource
URL cannot be openeddestination
cannot be written to- an IO error occurs during copying
-
normalize
public static String normalize(String path)
Normalize a path. Eliminates "/../" and "/./" in a string. Returnsnull
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.
-
catPath
public static String catPath(String lookupPath, String path)
Will concatenate 2 paths. Paths with
Eg.,..
will be properly handled./a/b/c + d = /a/b/d /a/b/c + ../d = /a/d
Thieved from Tomcat sources...
- Parameters:
lookupPath
- a pathpath
- the path to concatenate- Returns:
- The concatenated paths, or null if error occurs
-
resolveFile
public static File resolveFile(File baseFile, String filename)
Resolve a filefilename
to it's canonical form. Iffilename
is relative (doesn't start with/
), it will be resolved relative tobaseFile
, otherwise it is treated as a normal root-relative path.- Parameters:
baseFile
- Where to resolvefilename
from, iffilename
is relative.filename
- Absolute or relative file path to resolve.- Returns:
- The canonical
File
offilename
.
-
forceDelete
public static void forceDelete(String file) throws IOException
Delete a file. If file is directory delete it and all sub-directories.- Parameters:
file
- the file path- Throws:
IOException
- if any
-
forceDelete
public static void forceDelete(File file) throws IOException
Delete a file. If file is directory delete it and all sub-directories.- Parameters:
file
- a file- Throws:
IOException
- if any
-
forceDeleteOnExit
public static void forceDeleteOnExit(File file) throws IOException
Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.- Parameters:
file
- a file- Throws:
IOException
- if any
-
forceMkdir
public static void forceMkdir(File file) throws IOException
Make a directory.- Parameters:
file
- not null- Throws:
IOException
- If there already exists a file with specified name or the directory is unable to be createdIllegalArgumentException
- if the file contains illegal Windows characters under Windows OS.- See Also:
INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
-
deleteDirectory
public static void deleteDirectory(String directory) throws IOException
Recursively delete a directory.- Parameters:
directory
- a directory- Throws:
IOException
- if any
-
deleteDirectory
public static void deleteDirectory(File directory) throws IOException
Recursively delete a directory.- Parameters:
directory
- a directory- Throws:
IOException
- if any
-
cleanDirectory
public static void cleanDirectory(String directory) throws IOException
Clean a directory without deleting it.- Parameters:
directory
- a directory- Throws:
IOException
- if any
-
cleanDirectory
public static void cleanDirectory(File directory) throws IOException
Clean a directory without deleting it.- Parameters:
directory
- a directory- Throws:
IOException
- if any
-
sizeOfDirectory
public static long sizeOfDirectory(String directory)
Recursively count size of a directory.- Parameters:
directory
- a directory- Returns:
- size of directory in bytes.
-
sizeOfDirectory
public static long sizeOfDirectory(File directory)
Recursively count size of a directory.- Parameters:
directory
- a directory- Returns:
- size of directory in bytes.
-
getFiles
public static List<File> getFiles(File directory, String includes, String excludes) throws IOException
Return the files contained in the directory, using inclusion and exclusion Ant patterns, including the directory name in each of the files- Parameters:
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separated- Returns:
- a list of File objects
- Throws:
IOException
- io issue- See Also:
getFileNames(File, String, String, boolean)
-
getFiles
public static List<File> getFiles(File directory, String includes, String excludes, boolean includeBasedir) throws IOException
Return the files contained in the directory, using inclusion and exclusion Ant patterns- Parameters:
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each file- Returns:
- a list of File objects
- Throws:
IOException
- io issue- See Also:
getFileNames(File, String, String, boolean)
-
getFileNames
public static List<String> getFileNames(File directory, String includes, String excludes, boolean includeBasedir) throws IOException
Return a list of files as String depending options. This method use case sensitive file name.- Parameters:
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- Returns:
- a list of files as String
- Throws:
IOException
- io issue
-
getFileNames
public static List<String> getFileNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive) throws IOException
Return a list of files as String depending options.- Parameters:
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- Returns:
- a list of files as String
- Throws:
IOException
- io issue
-
getDirectoryNames
public static List<String> getDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir) throws IOException
Return a list of directories as String depending options. This method use case sensitive file name.- Parameters:
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- Returns:
- a list of directories as String
- Throws:
IOException
- io issue
-
getDirectoryNames
public static List<String> getDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive) throws IOException
Return a list of directories as String depending options.- Parameters:
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- Returns:
- a list of directories as String
- Throws:
IOException
- io issue
-
getFileAndDirectoryNames
public static List<String> getFileAndDirectoryNames(File directory, String includes, String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories) throws IOException
Return a list of files as String depending options.- Parameters:
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- Returns:
- a list of files as String
- Throws:
IOException
- io issue
-
copyDirectory
public static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException
Copy a directory to an other one.- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dir- Throws:
IOException
- if any
-
copyDirectory
public static void copyDirectory(File sourceDirectory, File destinationDirectory, String includes, String excludes) throws IOException
Copy a directory to an other one.- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dirincludes
- include patternexcludes
- exclude pattern- Throws:
IOException
- if any- See Also:
getFiles(File, String, String)
-
copyDirectoryLayout
public static void copyDirectoryLayout(File sourceDirectory, File destinationDirectory, String[] includes, String[] excludes) throws IOException
Copies a entire directory layout : no files will be copied only directories
Note:- It will include empty directories.
- The
sourceDirectory
must exists.
- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dirincludes
- include patternexcludes
- exclude pattern- Throws:
IOException
- if any- Since:
- 1.5.7
-
copyDirectoryStructure
public static void copyDirectoryStructure(File sourceDirectory, File destinationDirectory) throws IOException
Copies a entire directory structure.
Note:- It will include empty directories.
- The
sourceDirectory
must exists.
- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dir- Throws:
IOException
- if any
-
copyDirectoryStructureIfModified
public static void copyDirectoryStructureIfModified(File sourceDirectory, File destinationDirectory) throws IOException
Copies an entire directory structure but only source files with timestamp later than the destinations'.
Note:- It will include empty directories.
- The
sourceDirectory
must exists.
- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dir- Throws:
IOException
- if any
-
rename
public static void rename(File from, File to) throws IOException
Renames a file, even if that involves crossing file system boundaries.
This will remove
to
(if it exists), ensure thatto
's parent directory exists and movefrom
, which involves deletingfrom
as well.- Parameters:
from
- the file to moveto
- the new file name- Throws:
IOException
- if anything bad happens during this process. Note thatto
may have been deleted already when this happens.
-
createTempFile
public static File createTempFile(String prefix, String suffix, File parentDir)
Create a temporary file in a given directory.
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 number
This 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 numbersuffix
- 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.
-
copyFile
public static void copyFile(File from, File to, String encoding, FileUtils.FilterWrapper[] wrappers) throws IOException
If wrappers is null or empty, the file will be copy only ifto.lastModified() < from.lastModified()
- Parameters:
from
- the file to copyto
- the destination fileencoding
- the file output encoding (only if wrappers is not empty)wrappers
- array ofFileUtils.FilterWrapper
- Throws:
IOException
- if an IO error occurs during copying or filtering
-
copyFile
public static void copyFile(File from, File to, String encoding, FileUtils.FilterWrapper[] wrappers, boolean overwrite) throws IOException
If wrappers is null or empty, the file will be copy only ifto.lastModified() < from.lastModified()
, if overwrite is true- Parameters:
from
- the file to copyto
- the destination fileencoding
- the file output encoding (only if wrappers is not empty)wrappers
- array ofFileUtils.FilterWrapper
overwrite
- if true and wrappers is null or empty, the file will be copied even ifto.lastModified() < from.lastModified()
- Throws:
IOException
- if an IO error occurs during copying or filtering- Since:
- 1.5.2
-
loadFile
public static List<String> loadFile(File file) throws IOException
Note: the file content is read with platform encoding- Parameters:
file
- the file- Returns:
- a List containing every every line not starting with # and not empty
- Throws:
IOException
- if any
-
isValidWindowsFileName
public static boolean isValidWindowsFileName(File f)
For Windows OS, check if the file name contains any of the following characters:":", "*", "?", "\"", "<", ">", "|"
- 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
-
-