public class FileUtils
extends java.lang.Object
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 through removeExtension(java.lang.String)
html
-- retrievable through getExtension(java.lang.String)
File from a URL
, copy a
copy a File to another File
,
copy a URL's contents to a File
,
as well as methods to delete
and clean
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.Modifier and Type | Class and Description |
---|---|
static class |
FileUtils.FilterWrapper
Wrapper class for Filter.
|
Modifier and Type | Field and Description |
---|---|
private static long |
FILE_COPY_BUFFER_SIZE
The file copy buffer size (30 MB)
|
private static java.lang.String |
FS
The vm line separator
|
private static java.lang.String[] |
INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
Non-valid Characters for naming files, folders under Windows:
":", "*", "?", "\"", "<", ">", "|" |
private static int |
ONE_GB
The number of bytes in a gigabyte.
|
private static int |
ONE_KB
The number of bytes in a kilobyte.
|
private static int |
ONE_MB
The number of bytes in a megabyte.
|
Modifier | Constructor and Description |
---|---|
protected |
FileUtils()
protected constructor.
|
Modifier and Type | Method and Description |
---|---|
private static java.util.List<java.lang.String> |
blendFilesToList(java.util.List<java.lang.String> v,
java.lang.String... files)
Private helper method for getFilesFromExtension()
|
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.
|
private static void |
copyDirectoryStructure(java.io.File sourceDirectory,
java.io.File destinationDirectory,
java.io.File rootDestinationDirectory,
boolean onlyModifiedFiles) |
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 true
|
private static boolean |
copyFileIfModified(java.io.File source,
java.io.File destination)
Copy file from source to destination only if source timestamp is later than the destination timestamp.
|
static void |
copyFileToDirectory(java.io.File source,
java.io.File destinationDirectory)
Copy file from source to destination.
|
private static void |
copyFileToDirectoryIfModified(java.io.File source,
java.io.File destinationDirectory)
Copy file from source to destination only if source is newer than the target file.
|
private static void |
copyStreamToFile(java.io.InputStream source,
java.io.File destination)
Copies bytes from the
InputStream source to a file destination . |
static void |
copyURLToFile(java.net.URL source,
java.io.File destination)
Copies bytes from the URL
source to a file destination . |
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.
|
private static boolean |
deleteFile(java.io.File file)
Accommodate Windows bug encountered in both Sun and IBM JDKs.
|
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.
|
private static void |
doCopyFile(java.io.File source,
java.io.File destination) |
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 encoding
|
static 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.
|
private static java.lang.String |
fileRead(java.lang.String file,
java.lang.String 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.
|
private static java.util.List<java.lang.String> |
getFileNames(java.io.File directory,
java.lang.String includes,
java.lang.String excludes,
boolean includeBasedir,
boolean isCaseSensitive)
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 files
|
static 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 patterns
|
static 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.
|
(package private) static boolean |
isSymbolicLinkLegacy(java.io.File file)
Checks whether a given file is a symbolic link.
|
private static boolean |
isValidFile(java.lang.String file,
java.lang.String... extensions)
Checks to see if a file is of a particular type(s).
|
private static boolean |
isValidWindowsFileName(java.io.File f)
For Windows OS, check if the file name contains any of the following characters:
":", "*", "?", "\"", "<", ">", "|" |
static java.util.List<java.lang.String> |
loadFile(java.io.File file)
Note: the file content is read with platform encoding
|
static void |
mkdir(java.lang.String dir)
Simple way to make a directory
|
private static void |
mkdirsFor(java.io.File destination) |
static java.lang.String |
normalize(java.lang.String path)
Normalize a path.
|
private static int |
positiveRandom(java.util.Random rand) |
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 file
filename 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 a
URL to a File . |
static java.net.URL[] |
toURLs(java.io.File... files)
Convert the array of Files into a list of URLs.
|
private static final int ONE_KB
private static final int ONE_MB
private static final int ONE_GB
private static final long FILE_COPY_BUFFER_SIZE
private static final java.lang.String FS
private static final java.lang.String[] INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
":", "*", "?", "\"", "<", ">", "|"
@Nonnull public static java.lang.String[] getDefaultExcludes()
DirectoryScanner.DEFAULTEXCLUDES
@Nonnull public static java.util.List<java.lang.String> getDefaultExcludesAsList()
getDefaultExcludes()
@Nonnull public static java.lang.String getDefaultExcludesAsString()
DirectoryScanner.DEFAULTEXCLUDES
,
StringUtils.join(Object[], String)
@Nonnull public static java.lang.String dirname(@Nonnull java.lang.String filename)
filename
- the file path@Nonnull public static java.lang.String filename(@Nonnull java.lang.String filename)
filename
- the file path@Nonnull public static java.lang.String extension(@Nonnull java.lang.String filename)
filename
- the file pathpublic static boolean fileExists(@Nonnull java.lang.String fileName)
fileName
- the file path.@Nonnull public static java.lang.String fileRead(@Nonnull java.lang.String file) throws java.io.IOException
file
- the file pathjava.io.IOException
- if any@Nonnull private static java.lang.String fileRead(@Nonnull java.lang.String file, @Nullable java.lang.String encoding) throws java.io.IOException
file
- the file pathencoding
- the wanted encodingjava.io.IOException
- if any@Nonnull public static java.lang.String fileRead(@Nonnull java.io.File file) throws java.io.IOException
file
- the file pathjava.io.IOException
- if any@Nonnull public static java.lang.String fileRead(@Nonnull java.io.File file, @Nullable java.lang.String encoding) throws java.io.IOException
file
- the file pathencoding
- the wanted encodingjava.io.IOException
- if any@Nonnull public static java.lang.String[] fileReadArray(@Nonnull java.io.File file) throws java.io.IOException
file
- the file pathjava.io.IOException
- in case of failure.public static void fileAppend(@Nonnull java.lang.String fileName, @Nonnull java.lang.String data) throws java.io.IOException
fileName
- The path of the file to write.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileAppend(@Nonnull java.lang.String fileName, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.IOException
fileName
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileWrite(@Nonnull java.lang.String fileName, @Nonnull java.lang.String data) throws java.io.IOException
fileName
- The path of the file to write.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileWrite(@Nonnull java.lang.String fileName, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.IOException
fileName
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileWrite(@Nonnull java.io.File file, @Nullable java.lang.String encoding, @Nonnull java.lang.String data) throws java.io.IOException
file
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileWriteArray(@Nonnull java.io.File file, @Nullable java.lang.String... data) throws java.io.IOException
file
- The path of the file to write.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileWriteArray(@Nonnull java.io.File file, @Nullable java.lang.String encoding, @Nullable java.lang.String... data) throws java.io.IOException
file
- The path of the file to write.encoding
- The encoding of the file.data
- The content to write to the file.java.io.IOException
- if anypublic static void fileDelete(@Nonnull java.lang.String fileName)
fileName
- The path of the file to delete.public static java.lang.String[] getFilesFromExtension(@Nonnull java.lang.String directory, @Nonnull java.lang.String... extensions)
directory
- The path of the directory.extensions
- an array of expected extensions.@Nonnull private static java.util.List<java.lang.String> blendFilesToList(@Nonnull java.util.List<java.lang.String> v, @Nonnull java.lang.String... files)
private static boolean isValidFile(@Nonnull java.lang.String file, @Nonnull java.lang.String... extensions)
public static void mkdir(@Nonnull java.lang.String dir)
dir
- the directory to createjava.lang.IllegalArgumentException
- if the dir contains illegal Windows characters under Windows OS.INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
public static boolean contentEquals(@Nonnull java.io.File file1, @Nonnull java.io.File file2) throws java.io.IOException
file1
- the first filefile2
- the second filejava.io.IOException
- if any@Nullable public static java.io.File toFile(@Nullable java.net.URL url)
URL
to a File
.url
- File URL.File
object, or null
if the URL's protocol
is not file
@Nonnull public static java.net.URL[] toURLs(@Nonnull java.io.File... files) throws java.io.IOException
files
- the array of filesjava.io.IOException
- if an error occurs@Nonnull public static java.lang.String removeExtension(@Nonnull java.lang.String filename)
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename
- the path of the file@Nonnull public static java.lang.String getExtension(@Nonnull java.lang.String filename)
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
filename
- the path of the filepublic static void copyFileToDirectory(@Nonnull java.io.File source, @Nonnull java.io.File destinationDirectory) throws java.io.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.java.io.FileNotFoundException
- if source
isn't a normal file.java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.java.io.IOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error
occurs during copying.private static void copyFileToDirectoryIfModified(@Nonnull java.io.File source, @Nonnull java.io.File destinationDirectory) throws java.io.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.java.io.FileNotFoundException
- if source
isn't a normal file.java.lang.IllegalArgumentException
- if destinationDirectory
isn't a directory.java.io.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(@Nonnull java.io.File source, @Nonnull java.io.File destination) throws java.io.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).java.io.IOException
- if source
does not exist, destination
cannot be
written to, or an IO error occurs during copying.java.io.FileNotFoundException
- if destination
is a directoryprivate static void mkdirsFor(@Nonnull java.io.File destination)
private static void doCopyFile(@Nonnull java.io.File source, @Nonnull java.io.File destination) throws java.io.IOException
java.io.IOException
private static boolean copyFileIfModified(@Nonnull java.io.File source, @Nonnull java.io.File destination) throws java.io.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).java.io.IOException
- if source
does not exist, destination
cannot be
written to, or an IO error occurs during copying.public static void copyURLToFile(@Nonnull java.net.URL source, @Nonnull java.io.File destination) throws java.io.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).java.io.IOException
- if
source
URL cannot be openeddestination
cannot be written toprivate static void copyStreamToFile(@Nonnull @WillClose java.io.InputStream source, @Nonnull java.io.File destination) throws java.io.IOException
InputStream
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
- An InputStream
to copy bytes from. This stream is
guaranteed to be closed.destination
- A non-directory File
to write bytes to (possibly
overwriting).java.io.IOException
- if
source
URL cannot be openeddestination
cannot be written to@Nonnull public static java.lang.String normalize(@Nonnull java.lang.String path)
null
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
path
- the path to normalizenull
if too many ..'s.@Nonnull public static java.io.File resolveFile(java.io.File baseFile, @Nonnull java.lang.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(@Nonnull java.lang.String file) throws java.io.IOException
file
- the file pathjava.io.IOException
- if anypublic static void forceDelete(@Nonnull java.io.File file) throws java.io.IOException
file
- a filejava.io.IOException
- if anypublic static void delete(@Nonnull java.io.File file) throws java.io.IOException
file
- The file to deletejava.io.IOException
- If the file cannot be delted.public static boolean deleteLegacyStyle(@Nonnull java.io.File file)
file
- The file.private static boolean deleteFile(@Nonnull java.io.File file) throws java.io.IOException
file
- a filejava.io.IOException
- if anypublic static void forceMkdir(@Nonnull java.io.File file) throws java.io.IOException
file
- not nulljava.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.INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
public static void deleteDirectory(@Nonnull java.lang.String directory) throws java.io.IOException
directory
- a directoryjava.io.IOException
- if anypublic static void deleteDirectory(@Nonnull java.io.File directory) throws java.io.IOException
directory
- a directoryjava.io.IOException
- if anypublic static void cleanDirectory(@Nonnull java.io.File directory) throws java.io.IOException
directory
- a directoryjava.io.IOException
- if anypublic static long sizeOfDirectory(@Nonnull java.lang.String directory)
directory
- a directorypublic static long sizeOfDirectory(@Nonnull java.io.File directory)
directory
- a directory@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
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedjava.io.IOException
- in case of failure.getFileNames(File, String, String, boolean)
@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
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each filejava.io.IOException
- in case of failure.getFileNames(File, String, String, boolean)
@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
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 filejava.io.IOException
- in case of failure.@Nonnull private 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, boolean isCaseSensitive) throws java.io.IOException
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 sensitivejava.io.IOException
@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
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 filejava.io.IOException
- in case of failure.@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
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 sensitivejava.io.IOException
- in case of failure.@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)
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 directoriespublic static void copyDirectory(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory) throws java.io.IOException
sourceDirectory
- the source dirdestinationDirectory
- the target dirjava.io.IOException
- if anypublic 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
sourceDirectory
- the source dirdestinationDirectory
- the target dirincludes
- include patternexcludes
- exlucde patternjava.io.IOException
- if anygetFiles(File, String, String)
public static void copyDirectoryStructure(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory) throws java.io.IOException
sourceDirectory
must exists.
sourceDirectory
- the source dirdestinationDirectory
- the target dirjava.io.IOException
- if anyprivate static void copyDirectoryStructure(@Nonnull java.io.File sourceDirectory, @Nonnull java.io.File destinationDirectory, java.io.File rootDestinationDirectory, boolean onlyModifiedFiles) throws java.io.IOException
java.io.IOException
public static void rename(@Nonnull java.io.File from, @Nonnull java.io.File to) throws java.io.IOException
This will remove to
(if it exists), ensure that
to
's parent directory exists and move
from
, which involves deleting from
as
well.
from
- the file to moveto
- the new file namejava.io.IOException
- if anything bad happens during this process.
Note that to
may have been deleted already when this happens.public static java.io.File createTempFile(@Nonnull java.lang.String prefix, @Nonnull java.lang.String suffix, @Nullable java.io.File parentDir)
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.
prefix
- prefix before the random numbersuffix
- file extension; include the '.'parentDir
- Directory to create the temporary file in -java.io.tmpdir
used if not specificedprivate static int positiveRandom(java.util.Random rand)
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
from
- the file to copyto
- the destination fileencoding
- the file output encoding (only if wrappers is not empty)wrappers
- array of FileUtils.FilterWrapper
java.io.IOException
- if an IO error occurs during copying or filteringpublic 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
from
- the file to copyto
- the destination fileencoding
- the file output encoding (only if wrappers is not empty)wrappers
- array of FileUtils.FilterWrapper
overwrite
- if true and f wrappers is null or empty, the file will be copy enven if to.lastModified() <
from.lastModified()java.io.IOException
- if an IO error occurs during copying or filtering@Nonnull public static java.util.List<java.lang.String> loadFile(@Nonnull java.io.File file) throws java.io.IOException
file
- the filejava.io.IOException
- if anyprivate static boolean isValidWindowsFileName(@Nonnull java.io.File f)
":", "*", "?", "\"", "<", ">", "|"
f
- not null filefalse
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.INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
public static boolean isSymbolicLink(@Nonnull java.io.File file) throws java.io.IOException
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.
file
- the file to checkjava.io.IOException
- in case of failure.public static boolean isSymbolicLinkForSure(@Nonnull java.io.File file) throws java.io.IOException
file
- the file to checkjava.io.IOException
- in case of failure.static boolean isSymbolicLinkLegacy(@Nonnull java.io.File file) throws java.io.IOException
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. It also returns true for any file that has been reached via a symbolic link, if you decide to traverse into the symlink. As can be seen from the "return" clause of this method, there is really no guarantee of any sort from this method. Small wonder this ever got used for anything.
file
- the file to checkjava.io.IOException