public class FileUtils extends 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 | Constructor and Description |
---|---|
protected |
FileUtils()
protected constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(File directory)
Deprecated.
use
org.apache.commons.io.FileUtils.cleanDirectory() |
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 the contents of a directory into another one.
|
static void |
copyDirectory(File sourceDirectory,
File destinationDirectory,
String includes,
String excludes)
Copy the contents of a directory into another one.
|
static void |
copyDirectoryStructure(File sourceDirectory,
File destinationDirectory)
Deprecated.
use
org.apache.commons.io.FileUtils.copyDirectory() |
static void |
copyFile(File source,
File destination)
Deprecated.
use
java.nio.Files.copy(source.toPath(), destination.toPath(), CopyOptions.NOFOLLOW_LINKS,
CopyOptions.REPLACE_EXISTING) |
static void |
copyFile(File from,
File to,
String encoding,
FileUtils.FilterWrapper... wrappers)
If wrappers is null or empty, the file will be copied only if to.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 if to.lastModified() < from.lastModified() or if
overwrite is true
|
static void |
copyFileToDirectory(File source,
File destinationDirectory)
Deprecated.
use
org.apache.commons.io.FileUtils.copyFileToDirectory() |
static void |
copyURLToFile(URL source,
File destination)
Deprecated.
use
java.nio.Files.copy(source.openStream(), destination.toPath(),
CopyOptions.REPLACE_EXISTING) |
static File |
createSymbolicLink(File symlink,
File target)
Create a new symbolic link, possibly replacing an existing symbolic link.
|
static File |
createTempFile(String prefix,
String suffix,
File parentDir)
Deprecated.
use
java.nio.Files.createTempFile() |
static void |
delete(File file)
Deprecated.
use
java.nio.files.Files.delete(file.toPath()) |
static void |
deleteDirectory(File directory)
Deprecated.
use
org.apache.commons.io.FileUtils.deleteDirectory() |
static void |
deleteDirectory(String directory)
Deprecated.
use
org.apache.commons.io.FileUtils.deleteDirectory() |
static boolean |
deleteLegacyStyle(File file)
Deprecated.
use
java.nio.files.Files.delete(file.toPath()) |
static String |
dirname(String path)
Deprecated.
use
Paths.get(path).getParent().getName() |
static String |
extension(String path)
Deprecated.
use
org.apache.commons.io.FilenameUtils.getExtension |
static void |
fileAppend(String fileName,
String data)
Deprecated.
use
java.nio.files.Files.write(filename, data.getBytes(),
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
static void |
fileAppend(String fileName,
String encoding,
String data)
Deprecated.
use
java.nio.files.Files.write(filename, data.getBytes(encoding),
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
static void |
fileDelete(String fileName)
Deprecated.
use
Files.delete(Paths.get(fileName)) |
static boolean |
fileExists(String fileName)
Deprecated.
use
java.io.File.exists() |
static String |
filename(String path)
Deprecated.
use
Paths.get(path).getName() |
static String |
fileRead(File file)
Deprecated.
use
new String(java.nio.files.Files.readAllBytes(file.toPath())) |
static String |
fileRead(File file,
String encoding)
Deprecated.
use
new String(java.nio.files.Files.readAllBytes(file.toPath()), encoding) |
static String |
fileRead(String file)
Deprecated.
use
new String(java.nio.files.Files.readAllBytes(file)) |
static String[] |
fileReadArray(File file)
Deprecated.
use
java.nio.files.Files.readAllLines() |
static void |
fileWrite(File file,
String encoding,
String data)
Deprecated.
use
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE) |
static void |
fileWrite(String fileName,
String data)
Deprecated.
use
java.nio.files.Files.write(filename,
data.getBytes(), StandardOpenOption.CREATE) |
static void |
fileWrite(String fileName,
String encoding,
String data)
Deprecated.
use
java.nio.files.Files.write(Paths.get(filename),
data.getBytes(encoding), StandardOpenOption.CREATE) |
static void |
fileWriteArray(File file,
String... data)
Deprecated.
use
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE) |
static void |
fileWriteArray(File file,
String encoding,
String... data)
Deprecated.
use
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE) |
static void |
forceDelete(File file)
Deprecated.
use
org.apache.commons.io.FileUtils.deleteQuietly() |
static void |
forceDelete(String file)
Deprecated.
use
org.apache.commons.io.FileUtils.deleteQuietly() |
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 Strings.
|
static String |
getExtension(String filename)
Deprecated.
use
org.apache.commons.io.FilenameUtils.getExtension() |
static List<String> |
getFileAndDirectoryNames(File directory,
String includes,
String excludes,
boolean includeBasedir,
boolean isCaseSensitive,
boolean getFiles,
boolean getDirectories)
Return a list of file names as Strings.
|
static List<String> |
getFileNames(File directory,
String includes,
String excludes,
boolean includeBasedir)
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 files
|
static List<File> |
getFiles(File directory,
String includes,
String excludes,
boolean includeBasedir)
Return the files contained in the directory, using inclusion and exclusion Ant patterns
|
static String[] |
getFilesFromExtension(String directory,
String... extensions)
Given a directory and an array of extensions return an array of compliant files.
|
static boolean |
isSymbolicLink(File file)
Deprecated.
use
java.nio.file.Files.isSymbolicLink(file.toPath()) |
static boolean |
isSymbolicLinkForSure(File file)
Deprecated.
use
java.nio.file.Files.isSymbolicLink(file.toPath()) |
static List<String> |
loadFile(File file)
Deprecated.
assumes the platform default character set
|
static void |
mkdir(String dir)
Deprecated.
use
java.nio.file.Files.createDirectories(Paths.get(dir)) |
static String |
normalize(String path)
Deprecated.
use
org.apache.commons.io.FileNameUtils.normalize() |
static String |
removeExtension(String filename)
Deprecated.
use
org.apache.commons.io.FilenameUtils.removeExtension() |
static void |
rename(File from,
File to)
Deprecated.
use
java.nio.Files.move() |
static File |
resolveFile(File baseFile,
String filename)
Resolve a file
filename to it's canonical form. |
static long |
sizeOfDirectory(File directory)
Deprecated.
use
org.apache.commons.io.FileUtils.sizeOf() |
static long |
sizeOfDirectory(String directory)
Deprecated.
use
org.apache.commons.io.FileUtils.sizeOf() |
static File |
toFile(URL url)
Convert from a
URL to a File . |
static URL[] |
toURLs(File... files)
Convert the array of Files into a list of URLs.
|
@Nonnull public static String[] getDefaultExcludes()
DirectoryScanner.DEFAULTEXCLUDES
@Nonnull public static List<String> getDefaultExcludesAsList()
getDefaultExcludes()
@Nonnull public static String getDefaultExcludesAsString()
DirectoryScanner.DEFAULTEXCLUDES
,
StringUtils.join(Object[], String)
@Deprecated @Nonnull public static String dirname(@Nonnull String path)
Paths.get(path).getParent().getName()
path
- the file path@Deprecated @Nonnull public static String filename(@Nonnull String path)
Paths.get(path).getName()
path
- the file path@Deprecated @Nonnull public static String extension(@Nonnull String path)
org.apache.commons.io.FilenameUtils.getExtension
path
- the file path@Deprecated public static boolean fileExists(@Nonnull String fileName)
java.io.File.exists()
fileName
- the file path@Deprecated @Nonnull public static String fileRead(@Nonnull String file) throws IOException
new String(java.nio.files.Files.readAllBytes(file))
file
- the file pathIOException
- if any@Deprecated @Nonnull public static String fileRead(@Nonnull File file) throws IOException
new String(java.nio.files.Files.readAllBytes(file.toPath()))
file
- the file pathIOException
- if any@Deprecated @Nonnull public static String fileRead(@Nonnull File file, @Nullable String encoding) throws IOException
new String(java.nio.files.Files.readAllBytes(file.toPath()), encoding)
file
- the file pathencoding
- the wanted encodingIOException
- if any@Deprecated @Nonnull public static String[] fileReadArray(@Nonnull File file) throws IOException
java.nio.files.Files.readAllLines()
file
- the file pathIOException
- in case of failure@Deprecated public static void fileAppend(@Nonnull String fileName, @Nonnull String data) throws IOException
java.nio.files.Files.write(filename, data.getBytes(),
StandardOpenOption.APPEND, StandardOpenOption.CREATE)
fileName
- the path of the file to writedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileAppend(@Nonnull String fileName, @Nullable String encoding, @Nonnull String data) throws IOException
java.nio.files.Files.write(filename, data.getBytes(encoding),
StandardOpenOption.APPEND, StandardOpenOption.CREATE)
fileName
- the path of the file to writeencoding
- the encoding of the filedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileWrite(@Nonnull String fileName, @Nonnull String data) throws IOException
java.nio.files.Files.write(filename,
data.getBytes(), StandardOpenOption.CREATE)
fileName
- the path of the file to writedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileWrite(@Nonnull String fileName, @Nullable String encoding, @Nonnull String data) throws IOException
java.nio.files.Files.write(Paths.get(filename),
data.getBytes(encoding), StandardOpenOption.CREATE)
fileName
- the path of the file to writeencoding
- the encoding of the filedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileWrite(@Nonnull File file, @Nullable String encoding, @Nonnull String data) throws IOException
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE)
file
- the path of the file to writeencoding
- the encoding of the filedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileWriteArray(@Nonnull File file, @Nullable String... data) throws IOException
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE)
file
- the path of the file to writedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileWriteArray(@Nonnull File file, @Nullable String encoding, @Nullable String... data) throws IOException
java.nio.files.Files.write(file.toPath(),
data.getBytes(encoding), StandardOpenOption.CREATE)
file
- the path of the file to writeencoding
- the encoding of the filedata
- the content to write to the fileIOException
- if any@Deprecated public static void fileDelete(@Nonnull String fileName)
Files.delete(Paths.get(fileName))
fileName
- the path of the file to deletepublic static String[] getFilesFromExtension(@Nonnull String directory, @Nonnull String... extensions)
directory
- the path of the directoryextensions
- an array of expected extensions@Deprecated public static void mkdir(@Nonnull String dir)
java.nio.file.Files.createDirectories(Paths.get(dir))
dir
- the directory to createIllegalArgumentException
- if the dir contains illegal Windows characters under Windows OSINVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
public static boolean contentEquals(@Nonnull File file1, @Nonnull File file2) throws IOException
file1
- the first filefile2
- the second fileIOException
- if any@Nullable public static File toFile(@Nullable URL url)
URL
to a File
.url
- file URLFile
object, or null
if the URL's protocol
is not file
@Nonnull public static URL[] toURLs(@Nonnull File... files) throws IOException
files
- the array of filesIOException
- if an error occurs@Deprecated @Nonnull public static String removeExtension(@Nonnull String filename)
org.apache.commons.io.FilenameUtils.removeExtension()
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename
- the path of the file@Deprecated @Nonnull public static String getExtension(@Nonnull String filename)
org.apache.commons.io.FilenameUtils.getExtension()
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
filename
- the path of the file@Deprecated public static void copyFileToDirectory(@Nonnull File source, @Nonnull File destinationDirectory) throws IOException
org.apache.commons.io.FileUtils.copyFileToDirectory()
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 copydestinationDirectory
- a directory to copy source
intoFileNotFoundException
- if source
isn't a normal fileIllegalArgumentException
- if destinationDirectory
isn't a directoryIOException
- if source
does not exist, the file in
destinationDirectory
cannot be written to, or an IO error
occurs during copying.@Deprecated public static void copyFile(@Nonnull File source, @Nonnull File destination) throws IOException
java.nio.Files.copy(source.toPath(), destination.toPath(), CopyOptions.NOFOLLOW_LINKS,
CopyOptions.REPLACE_EXISTING)
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 fromdestination
- a non-directory File
to write bytes to (possibly
overwriting)IOException
- if source
does not exist, destination
cannot be
written to, or an IO error occurs during copyingFileNotFoundException
- if destination
is a directorypublic static void copyURLToFile(@Nonnull URL source, @Nonnull File destination) throws IOException
java.nio.Files.copy(source.openStream(), destination.toPath(),
CopyOptions.REPLACE_EXISTING)
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).IOException
- if
source
URL cannot be openeddestination
cannot be written to@Deprecated @Nonnull public static String normalize(@Nonnull String path)
org.apache.commons.io.FileNameUtils.normalize()
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 File resolveFile(File baseFile, @Nonnull 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 resolveFile
of filename
@Deprecated public static void forceDelete(@Nonnull String file) throws IOException
org.apache.commons.io.FileUtils.deleteQuietly()
file
- the file pathIOException
- if any@Deprecated public static void forceDelete(@Nonnull File file) throws IOException
org.apache.commons.io.FileUtils.deleteQuietly()
file
- a fileIOException
- if any@Deprecated public static void delete(@Nonnull File file) throws IOException
java.nio.files.Files.delete(file.toPath())
file
- the file to deleteIOException
- if the file cannot be deleted@Deprecated public static boolean deleteLegacyStyle(@Nonnull File file)
java.nio.files.Files.delete(file.toPath())
file
- the filepublic static void forceMkdir(@Nonnull File file) throws IOException
file
- not nullIOException
- if a file already exists with the specified name or the directory is unable to be createdIllegalArgumentException
- if the file contains illegal Windows characters under Windows OS.INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
@Deprecated public static void deleteDirectory(@Nonnull String directory) throws IOException
org.apache.commons.io.FileUtils.deleteDirectory()
directory
- a directoryIOException
- if any@Deprecated public static void deleteDirectory(@Nonnull File directory) throws IOException
org.apache.commons.io.FileUtils.deleteDirectory()
directory
- a directoryIOException
- if any@Deprecated public static void cleanDirectory(@Nonnull File directory) throws IOException
org.apache.commons.io.FileUtils.cleanDirectory()
directory
- a directoryIOException
- if any. This can leave cleaning in a half-finished state where
some but not all files have been deleted.@Deprecated public static long sizeOfDirectory(@Nonnull String directory)
org.apache.commons.io.FileUtils.sizeOf()
directory
- a directory@Deprecated public static long sizeOfDirectory(@Nonnull File directory)
org.apache.commons.io.FileUtils.sizeOf()
directory
- a directory@Nonnull public static List<File> getFiles(@Nonnull File directory, @Nullable String includes, @Nullable String excludes) throws IOException
directory
- the directory to scanincludes
- the Ant includes pattern, comma separatedexcludes
- the Ant excludes pattern, comma separatedIOException
- in case of failure.getFileNames(File, String, String, boolean)
@Nonnull public static List<File> getFiles(@Nonnull File directory, @Nullable String includes, @Nullable String excludes, boolean includeBasedir) throws IOException
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each fileIOException
- in case of failure.getFileNames(File, String, String, boolean)
@Nonnull public static List<String> getFileNames(@Nonnull File directory, @Nullable String includes, @Nullable String excludes, boolean includeBasedir) throws IOException
directory
- the directory to scanincludes
- the Ant includes pattern, comma separatedexcludes
- the Ant excludes pattern, comma separatedincludeBasedir
- true to include the base directory in each String of fileIOException
- in case of failure@Nonnull public static List<String> getDirectoryNames(@Nonnull File directory, @Nullable String includes, @Nullable String excludes, boolean includeBasedir) throws IOException
directory
- the directory to scanincludes
- the Ant includes pattern, comma separatedexcludes
- the Ant excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileIOException
- in case of failure.@Nonnull public static List<String> getDirectoryNames(@Nonnull File directory, @Nullable String includes, @Nullable String excludes, boolean includeBasedir, boolean isCaseSensitive) throws IOException
directory
- the directory to scanincludes
- the Ant includes pattern, comma separatedexcludes
- the Ant excludes pattern, comma separatedincludeBasedir
- true to include the base directory in each String of fileisCaseSensitive
- true if case sensitiveIOException
- in case of failure@Nonnull public static List<String> getFileAndDirectoryNames(File directory, @Nullable String includes, @Nullable String excludes, boolean includeBasedir, boolean isCaseSensitive, boolean getFiles, boolean getDirectories)
directory
- the directory to scanincludes
- the Ant includes pattern, comma separatedexcludes
- the Ant excludes pattern, comma separatedincludeBasedir
- true to include the base directory in each String of fileisCaseSensitive
- true if case sensitivegetFiles
- true to include regular filesgetDirectories
- true to include directoriespublic static void copyDirectory(@Nonnull File sourceDirectory, @Nonnull File destinationDirectory) throws IOException
sourceDirectory
- the source directorydestinationDirectory
- the target directoryIOException
- if anypublic static void copyDirectory(@Nonnull File sourceDirectory, @Nonnull File destinationDirectory, @Nullable String includes, @Nullable String excludes) throws IOException
sourceDirectory
- the source directorydestinationDirectory
- the target directoryincludes
- Ant include patternexcludes
- Ant exclude patternIOException
- if anygetFiles(File, String, String)
@Deprecated public static void copyDirectoryStructure(@Nonnull File sourceDirectory, @Nonnull File destinationDirectory) throws IOException
org.apache.commons.io.FileUtils.copyDirectory()
sourceDirectory
must exist.
sourceDirectory
- the source dirdestinationDirectory
- the target dirIOException
- if any@Deprecated public static void rename(@Nonnull File from, @Nonnull File to) throws IOException
java.nio.Files.move()
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 nameIOException
- if anything bad happens during this process.
Note that to
may have been deleted already when this happens.@Deprecated public static File createTempFile(@Nonnull String prefix, @Nonnull String suffix, @Nullable File parentDir)
java.nio.Files.createTempFile()
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)
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 automatically delete 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 specifiedpublic static void copyFile(@Nonnull File from, @Nonnull File to, @Nullable String encoding, @Nullable FileUtils.FilterWrapper... wrappers) throws IOException
from
- the file to copyto
- the destination fileencoding
- the file output encoding (only if wrappers is not empty)wrappers
- array of FileUtils.FilterWrapper
IOException
- if an IO error occurs during copying or filteringpublic static void copyFile(@Nonnull File from, @Nonnull File to, @Nullable String encoding, @Nullable FileUtils.FilterWrapper[] wrappers, boolean overwrite) throws 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 wrappers is null or empty, the file will be copied even if
to.lastModified() < from.lastModified()IOException
- if an IO error occurs during copying or filtering@Deprecated @Nonnull public static List<String> loadFile(@Nonnull File file) throws IOException
file
- the fileIOException
- if any@Deprecated public static boolean isSymbolicLink(@Nonnull File file) throws IOException
java.nio.file.Files.isSymbolicLink(file.toPath())
file
- the file to checkIOException
- in case of failure.@Deprecated public static boolean isSymbolicLinkForSure(@Nonnull File file) throws IOException
java.nio.file.Files.isSymbolicLink(file.toPath())
file
- the file to checkIOException
- in case of failure@Nonnull public static File createSymbolicLink(@Nonnull File symlink, @Nonnull File target) throws IOException
symlink
- the link nametarget
- the targetIOException
- in case of an errorjava.nio.file.Files.createSymbolicLink(Path)} which creates a new
symbolic link but does not replace exsiting symbolic links
Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.