Class FileUtils
- java.lang.Object
-
- org.apache.maven.shared.utils.io.FileUtils
-
public class FileUtils extends java.lang.Object
This class provides basic facilities for manipulating files and file paths.Path-related methods
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/index
-- retrievable throughremoveExtension(java.lang.String)
html
-- retrievable throughgetExtension(java.lang.String)
File-related methods
There are methods to create aFile from a URL
, copy a copy aFile to another File
, copy aURL's contents to a File
, as well as methods todelete
andclean
a directory. CommonFile
manipulation routines. Taken from the commons-utils repo. Also code from Alexandria's FileUtils. And from Avalon Excalibur's IO. And from Ant.- Version:
- $Id$
- Author:
- Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileUtils.FilterWrapper
Wrapper class for Filter.
-
Constructor Summary
Constructors Modifier Constructor Description protected
FileUtils()
protected constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
cleanDirectory(java.io.File 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)
Copy a directory to an other one.static void
copyDirectory(java.io.File sourceDirectory, java.io.File destinationDirectory, java.lang.String includes, java.lang.String excludes)
Copy a directory to an other one.static void
copyDirectoryStructure(java.io.File sourceDirectory, java.io.File destinationDirectory)
Copies a entire directory structure.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)
If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified()static void
copyFile(java.io.File from, java.io.File to, java.lang.String encoding, FileUtils.FilterWrapper[] wrappers, boolean overwrite)
If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if overwrite is truestatic void
copyFileToDirectory(java.io.File source, java.io.File destinationDirectory)
Copy file from source to destination.static void
copyURLToFile(java.net.URL source, java.io.File destination)
Copies bytes from the URLsource
to a filedestination
.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
delete(java.io.File file)
deletes a file.static void
deleteDirectory(java.io.File directory)
Recursively delete a directory.static void
deleteDirectory(java.lang.String directory)
Recursively delete a directory.static boolean
deleteLegacyStyle(java.io.File file)
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)
Note: the file content is read with platform encodingstatic java.lang.String
fileRead(java.io.File file, java.lang.String encoding)
static java.lang.String
fileRead(java.lang.String file)
Note: the file content is read with platform encoding.static java.lang.String[]
fileReadArray(java.io.File file)
static void
fileWrite(java.io.File file, java.lang.String encoding, java.lang.String data)
Writes data to a file.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
fileWriteArray(java.io.File file, java.lang.String... data)
Writes String array data to a file in the systems default encoding.static void
fileWriteArray(java.io.File file, java.lang.String encoding, java.lang.String... data)
Writes String array 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
forceMkdir(java.io.File file)
Make a directory.static java.lang.String[]
getDefaultExcludes()
static java.util.List<java.lang.String>
getDefaultExcludesAsList()
static java.lang.String
getDefaultExcludesAsString()
static java.util.List<java.lang.String>
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<java.lang.String>
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.util.List<java.lang.String>
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<java.lang.String>
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<java.io.File>
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 filesstatic java.util.List<java.io.File>
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 patternsstatic 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 boolean
isSymbolicLink(java.io.File file)
Checks whether a given file is a symbolic link.static boolean
isSymbolicLinkForSure(java.io.File file)
Checks whether a given file is a symbolic link.static java.util.List<java.lang.String>
loadFile(java.io.File file)
Note: the file content is read with platform encodingstatic void
mkdir(java.lang.String dir)
Simple way to make a directorystatic 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 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 filefilename
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 aURL
to aFile
.static java.net.URL[]
toURLs(java.io.File... files)
Convert the array of Files into a list of URLs.
-
-
-
Method Detail
-
getDefaultExcludes
@Nonnull public static java.lang.String[] getDefaultExcludes()
- Returns:
- the default excludes pattern
- See Also:
DirectoryScanner.DEFAULTEXCLUDES
-
getDefaultExcludesAsList
@Nonnull public static java.util.List<java.lang.String> getDefaultExcludesAsList()
- Returns:
- the default excludes pattern as list.
- See Also:
getDefaultExcludes()
-
getDefaultExcludesAsString
@Nonnull public static java.lang.String getDefaultExcludesAsString()
- Returns:
- the default excludes pattern as comma separated string.
- See Also:
DirectoryScanner.DEFAULTEXCLUDES
,StringUtils.join(Object[], String)
-
dirname
@Nonnull public static java.lang.String dirname(@Nonnull java.lang.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
@Nonnull public static java.lang.String filename(@Nonnull java.lang.String filename)
Returns the filename portion of a file specification string.- Parameters:
filename
- the file path- Returns:
- The filename string with extension.
-
extension
@Nonnull public static java.lang.String extension(@Nonnull java.lang.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(@Nonnull java.lang.String fileName)
Check if a file exits.- Parameters:
fileName
- the file path.- Returns:
- true if file exists.
-
fileRead
@Nonnull public static java.lang.String fileRead(@Nonnull java.lang.String file) throws java.io.IOException
Note: the file content is read with platform encoding.- Parameters:
file
- the file path- Returns:
- the file content using the platform encoding.
- Throws:
java.io.IOException
- if any
-
fileRead
@Nonnull public static java.lang.String fileRead(@Nonnull java.io.File file) throws java.io.IOException
Note: the file content is read with platform encoding- Parameters:
file
- the file path- Returns:
- the file content using the platform encoding.
- Throws:
java.io.IOException
- if any
-
fileRead
@Nonnull public static java.lang.String fileRead(@Nonnull java.io.File file, @Nullable java.lang.String encoding) throws java.io.IOException
- Parameters:
file
- the file pathencoding
- the wanted encoding- Returns:
- the file content using the specified encoding.
- Throws:
java.io.IOException
- if any
-
fileReadArray
@Nonnull public static java.lang.String[] fileReadArray(@Nonnull java.io.File file) throws java.io.IOException
- Parameters:
file
- the file path- Returns:
- the file content lines as String[] using the systems default encoding. An empty List if the file didn't exist.
- Throws:
java.io.IOException
- in case of failure.
-
fileAppend
public static void fileAppend(@Nonnull java.lang.String fileName, @Nonnull java.lang.String data) throws java.io.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:
java.io.IOException
- if any
-
fileAppend
public static void fileAppend(@Nonnull java.lang.String fileName, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.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:
java.io.IOException
- if any
-
fileWrite
public static void fileWrite(@Nonnull java.lang.String fileName, @Nonnull java.lang.String data) throws java.io.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:
java.io.IOException
- if any
-
fileWrite
public static void fileWrite(@Nonnull java.lang.String fileName, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.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:
java.io.IOException
- if any
-
fileWrite
public static void fileWrite(@Nonnull java.io.File file, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.IOException
Writes data to a file. The file will be created if it does not exist.- Parameters:
file
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.- Throws:
java.io.IOException
- if any
-
fileWriteArray
public static void fileWriteArray(@Nonnull java.io.File file, @Nullable java.lang.String... data) throws java.io.IOException
Writes String array data to a file in the systems default encoding. The file will be created if it does not exist.- Parameters:
file
- The path of the file to write.data
- The content to write to the file.- Throws:
java.io.IOException
- if any
-
fileWriteArray
public static void fileWriteArray(@Nonnull java.io.File file, @Nullable java.lang.String encoding, @Nullable java.lang.String... data) throws java.io.IOException
Writes String array data to a file. The file will be created if it does not exist.- Parameters:
file
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.- Throws:
java.io.IOException
- if any
-
fileDelete
public static void fileDelete(@Nonnull java.lang.String fileName)
Deletes a file.- Parameters:
fileName
- The path of the file to delete.
-
getFilesFromExtension
public static java.lang.String[] getFilesFromExtension(@Nonnull java.lang.String directory, @Nonnull java.lang.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(@Nonnull java.lang.String dir)
Simple way to make a directory- Parameters:
dir
- the directory to create- Throws:
java.lang.IllegalArgumentException
- if the dir contains illegal Windows characters under Windows OS.- See Also:
INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
-
contentEquals
public static boolean contentEquals(@Nonnull java.io.File file1, @Nonnull java.io.File file2) throws java.io.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:
java.io.IOException
- if any
-
toFile
@Nullable public static java.io.File toFile(@Nullable java.net.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
@Nonnull public static java.net.URL[] toURLs(@Nonnull java.io.File... files) throws java.io.IOException
Convert the array of Files into a list of URLs.- Parameters:
files
- the array of files- Returns:
- the array of URLs
- Throws:
java.io.IOException
- if an error occurs
-
removeExtension
@Nonnull public static java.lang.String removeExtension(@Nonnull java.lang.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
@Nonnull public static java.lang.String getExtension(@Nonnull java.lang.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
-
copyFileToDirectory
public static void copyFileToDirectory(@Nonnull java.io.File source, @Nonnull java.io.File destinationDirectory) throws java.io.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:
java.io.FileNotFoundException
- ifsource
isn't a normal file.java.lang.IllegalArgumentException
- ifdestinationDirectory
isn't a directory.java.io.IOException
- ifsource
does not exist, the file indestinationDirectory
cannot be written to, or an IO error occurs during copying.
-
copyFile
public static void copyFile(@Nonnull java.io.File source, @Nonnull java.io.File destination) throws java.io.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:
java.io.IOException
- ifsource
does not exist,destination
cannot be written to, or an IO error occurs during copying.java.io.FileNotFoundException
- ifdestination
is a directory
-
copyURLToFile
public static void copyURLToFile(@Nonnull java.net.URL source, @Nonnull java.io.File destination) throws java.io.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:
java.io.IOException
- ifsource
URL cannot be openeddestination
cannot be written to- an IO error occurs during copying
-
normalize
@Nonnull public static java.lang.String normalize(@Nonnull java.lang.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.
-
resolveFile
@Nonnull public static java.io.File resolveFile(java.io.File baseFile, @Nonnull java.lang.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(@Nonnull java.lang.String file) throws java.io.IOException
Delete a file. If file is directory delete it and all sub-directories.- Parameters:
file
- the file path- Throws:
java.io.IOException
- if any
-
forceDelete
public static void forceDelete(@Nonnull java.io.File file) throws java.io.IOException
Delete a file. If file is directory delete it and all sub-directories.- Parameters:
file
- a file- Throws:
java.io.IOException
- if any
-
delete
public static void delete(@Nonnull java.io.File file) throws java.io.IOException
deletes a file.- Parameters:
file
- The file to delete- Throws:
java.io.IOException
- If the file cannot be delted.
-
deleteLegacyStyle
public static boolean deleteLegacyStyle(@Nonnull java.io.File file)
- Parameters:
file
- The file.- Returns:
- true / false
-
forceMkdir
public static void forceMkdir(@Nonnull java.io.File file) throws java.io.IOException
Make a directory.- Parameters:
file
- not null- Throws:
java.io.IOException
- If there already exists a file with specified name or the directory is unable to be createdjava.lang.IllegalArgumentException
- if the file contains illegal Windows characters under Windows OS.- See Also:
INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
-
deleteDirectory
public static void deleteDirectory(@Nonnull java.lang.String directory) throws java.io.IOException
Recursively delete a directory.- Parameters:
directory
- a directory- Throws:
java.io.IOException
- if any
-
deleteDirectory
public static void deleteDirectory(@Nonnull java.io.File directory) throws java.io.IOException
Recursively delete a directory.- Parameters:
directory
- a directory- Throws:
java.io.IOException
- if any
-
cleanDirectory
public static void cleanDirectory(@Nonnull java.io.File directory) throws java.io.IOException
Clean a directory without deleting it.- Parameters:
directory
- a directory- Throws:
java.io.IOException
- if any
-
sizeOfDirectory
public static long sizeOfDirectory(@Nonnull java.lang.String directory)
Recursively count size of a directory.- Parameters:
directory
- a directory- Returns:
- size of directory in bytes.
-
sizeOfDirectory
public static long sizeOfDirectory(@Nonnull java.io.File directory)
Recursively count size of a directory.- Parameters:
directory
- a directory- Returns:
- size of directory in bytes.
-
getFiles
@Nonnull public static java.util.List<java.io.File> getFiles(@Nonnull java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes) throws java.io.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:
java.io.IOException
- in case of failure.- See Also:
getFileNames(File, String, String, boolean)
-
getFiles
@Nonnull public static java.util.List<java.io.File> getFiles(@Nonnull java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes, boolean includeBasedir) throws java.io.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:
java.io.IOException
- in case of failure.- See Also:
getFileNames(File, String, String, boolean)
-
getFileNames
@Nonnull public static java.util.List<java.lang.String> getFileNames(@Nonnull java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes, boolean includeBasedir) throws java.io.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:
java.io.IOException
- in case of failure.
-
getDirectoryNames
@Nonnull public static java.util.List<java.lang.String> getDirectoryNames(@Nonnull java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes, boolean includeBasedir) throws java.io.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:
java.io.IOException
- in case of failure.
-
getDirectoryNames
@Nonnull public static java.util.List<java.lang.String> getDirectoryNames(@Nonnull java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive) throws java.io.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:
java.io.IOException
- in case of failure.
-
getFileAndDirectoryNames
@Nonnull public static java.util.List<java.lang.String> getFileAndDirectoryNames(java.io.File directory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories)
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
-
copyDirectory
public static void copyDirectory(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory) throws java.io.IOException
Copy a directory to an other one.- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dir- Throws:
java.io.IOException
- if any
-
copyDirectory
public static void copyDirectory(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory, @Nullable java.lang.String includes, @Nullable java.lang.String excludes) throws java.io.IOException
Copy a directory to an other one.- Parameters:
sourceDirectory
- the source dirdestinationDirectory
- the target dirincludes
- include patternexcludes
- exlucde pattern- Throws:
java.io.IOException
- if any- See Also:
getFiles(File, String, String)
-
copyDirectoryStructure
public static void copyDirectoryStructure(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory) throws java.io.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:
java.io.IOException
- if any
-
rename
public static void rename(@Nonnull java.io.File from, @Nonnull java.io.File to) throws java.io.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:
java.io.IOException
- if anything bad happens during this process. Note thatto
may have been deleted already when this happens.
-
createTempFile
public static java.io.File createTempFile(@Nonnull java.lang.String prefix, @Nonnull java.lang.String suffix, @Nullable java.io.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 numberThis 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(@Nonnull java.io.File from, @Nonnull java.io.File to, @Nullable java.lang.String encoding, @Nullable FileUtils.FilterWrapper... wrappers) throws java.io.IOException
If wrappers is null or empty, the file will be copy only if to.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:
java.io.IOException
- if an IO error occurs during copying or filtering
-
copyFile
public static void copyFile(@Nonnull java.io.File from, @Nonnull java.io.File to, @Nullable java.lang.String encoding, @Nullable FileUtils.FilterWrapper[] wrappers, boolean overwrite) throws java.io.IOException
If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or 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 f wrappers is null or empty, the file will be copy enven if to.lastModified() < from.lastModified()- Throws:
java.io.IOException
- if an IO error occurs during copying or filtering
-
loadFile
@Nonnull public static java.util.List<java.lang.String> loadFile(@Nonnull java.io.File file) throws java.io.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:
java.io.IOException
- if any
-
isSymbolicLink
public static boolean isSymbolicLink(@Nonnull java.io.File file) throws java.io.IOException
Checks whether a given file is a symbolic link. This only works reliably on java7 and higher. For earlier version we use a highly crappy heuristic that mostly does not work.It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are identical - this may lead to false positives on some platforms.
- Parameters:
file
- the file to check- Returns:
- true if symbolic link false otherwise.
- Throws:
java.io.IOException
- in case of failure.
-
isSymbolicLinkForSure
public static boolean isSymbolicLinkForSure(@Nonnull java.io.File file) throws java.io.IOException
Checks whether a given file is a symbolic link.- Parameters:
file
- the file to check- Returns:
- true if and only if we reliably can say this is a symlink. This will always return false for java versions prior to 1.7.
- Throws:
java.io.IOException
- in case of failure.
-
-