Top Description Fields Constructors Methods
org.apache.tools.ant.util

public Class FileUtils

extends Object
Class Inheritance
Imports
java.io.File, .FilenameFilter, .IOException, .InputStream, .InputStreamReader, .OutputStream, .Reader, .Writer, java.net.HttpURLConnection, .JarURLConnection, .MalformedURLException, .URL, .URLConnection, java.nio.channels.Channel, java.nio.file.Files, .NoSuchFileException, .Path, .Paths, .StandardOpenOption, java.nio.file.attribute.FileAttribute, .PosixFileAttributeView, .PosixFilePermission, .PosixFilePermissions, java.lang.UnsupportedOperationException, java.text.DecimalFormat, java.util.ArrayList, .Arrays, .EnumSet, .List, .Locale, .Optional, .Random, .Set, .Stack, .StringTokenizer, .Vector, java.util.jar.JarFile, java.util.stream.Collectors, org.apache.tools.ant.BuildException, .MagicNames, .PathTokenizer, .Project, org.apache.tools.ant.launch.Locator, org.apache.tools.ant.taskdefs.condition.Os, org.apache.tools.ant.types.FilterChain, .FilterSetCollection, org.apache.tools.ant.types.resources.FileResource

This class also encapsulates methods which allow Files to be referred to using abstract path names which are translated to native system file paths at runtime as well as copying files or setting their last modification time.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
private final Object
cacheFromUriLock

A one item cache for fromUri.

private String
private String
private static final int
private static final int
public static final long
FAT_FILE_TIMESTAMP_GRANULARITY

The granularity of timestamps under FAT.

private static final FileAttribute<?>[]
public static final long
NTFS_FILE_TIMESTAMP_GRANULARITY

The granularity of timestamps under the NT File System.

private static final boolean
private static final boolean
private static final boolean
private static final boolean
private static final FileUtils
private static Random
private static final FileAttribute<?>[]
private static final FileAttribute<?>[]
public static final long
UNIX_FILE_TIMESTAMP_GRANULARITY

The granularity of timestamps under Unix.

Constructor Summary

AccessConstructor and Description
protected
FileUtils()

Empty constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

boolean
areSame
(File
File
f1
,
File
File
f2
)

Are the two File instances pointing to the same object on the file system?

public static void
close(Writer
output writer, can be null.
device
)

Close a Writer without throwing any exception if something went wrong.

public static void
close(Reader
Reader, can be null.
device
)

Close a Reader without throwing any exception if something went wrong.

public static void
close(OutputStream
stream, can be null.
device
)

Close a stream without throwing any exception if something went wrong.

public static void
close(InputStream
stream, can be null.
device
)

Close a stream without throwing any exception if something went wrong.

public static void
close(Channel
channel, can be null.
device
)

Close a Channel without throwing any exception if something went wrong.

public static void
close(URLConnection
connection, can be null
conn
)

Closes an URLConnection if its concrete implementation provides a way to close it that Ant knows of.

public static void
close(AutoCloseable
AutoCloseable, can be null.
ac
)

Close an AutoCloseable without throwing any exception if something went wrong.

public boolean

Returns:

true if the content of the files is the same.
contentEquals
(File
the file whose content is to be compared.
f1
,
File
the other file whose content is to be compared.
f2
)

Compares the contents of two files.

public boolean

Returns:

true if the content of the files is the same.
contentEquals
(File
the file whose content is to be compared.
f1
,
File
the other file whose content is to be compared.
f2
,
boolean
true if the file is to be treated as a text file and differences in kind of line break are to be ignored.
textfile
)

Compares the contents of two files.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
)

Convenience method to copy a file from a source to a destination.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used and if source files may overwrite newer destination files.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read and write the files.
encoding
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read and write the files.
encoding
,
Project
the project instance.
project
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(String
Name of file to copy from. Must not be null.
sourceFile
,
String
Name of file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read the files.
inputEncoding
,
String
the encoding used to write the files.
outputEncoding
,
Project
the project instance.
project
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
)

Convenience method to copy a file from a source to a destination.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used and if source files may overwrite newer destination files.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read and write the files.
encoding
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if source files may overwrite newer destination files, the last modified time of destFile file should be made equal to the last modified time of sourceFile and which character encoding to assume.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read and write the files.
encoding
,
Project
the project instance.
project
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
String
the encoding used to read the files.
inputEncoding
,
String
the encoding used to write the files.
outputEncoding
,
Project
the project instance.
project
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
boolean
whether to append to the destination file.
append
,
String
the encoding used to read the files.
inputEncoding
,
String
the encoding used to write the files.
outputEncoding
,
Project
the project instance.
project
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public void
copyFile(File
the file to copy from. Must not be null.
sourceFile
,
File
the file to copy to. Must not be null.
destFile
,
FilterSetCollection
the collection of filters to apply to this copy.
filters
,
Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
boolean
Whether or not the destination file should be overwritten if it already exists.
overwrite
,
boolean
Whether or not the last modified time of the resulting file should be set to that of the source file.
preserveLastModified
,
boolean
whether to append to the destination file.
append
,
String
the encoding used to read the files.
inputEncoding
,
String
the encoding used to write the files.
outputEncoding
,
Project
the project instance.
project
,
boolean
whether to overwrite read-only destination files.
force
)

Convenience method to copy a file from a source to a destination specifying if token filtering must be used, if filter chains must be used, if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.

public boolean

Returns:

true if the file did not exist already.
createNewFile
(File
the file to be created.
f
)

This was originally an emulation of File.createNewFile for JDK 1.1, but it is now implemented using that method (Ant 1.6.3 onwards).

public boolean

Returns:

true if the file did not exist already.
createNewFile
(File
the file to be created.
f
,
boolean
boolean whether to create parent directories.
mkdirs
)

Create a new file, optionally creating parent directories.

public File

Returns:

a File reference to the new, nonexistent temporary file.
createTempFile
(String
file name prefix.
prefix
,
String
file extension; include the '.'.
suffix
,
File
Directory to create the temporary file in; java.io.tmpdir used if not specified.
parentDir
)

Deprecated since ant 1.7.1 use createTempFile(Project, String, String, File, boolean, boolean) instead.
Create a File object for a temporary file in a given directory.
public File

Returns:

a File reference to the new temporary file.
createTempFile
(String
file name prefix.
prefix
,
String
file extension; include the '.'.
suffix
,
File
Directory to create the temporary file in; java.io.tmpdir used if not specified.
parentDir
,
boolean
whether to set the tempfile for deletion on normal VM exit.
deleteOnExit
,
boolean
true if the file must actually be created. If false chances exist that a file with the same name is created in the time between invoking this method and the moment the file is actually created. If possible set to true.
createFile
)

Deprecated since Ant 1.10.8 use createTempFile(Project, String, String, File, boolean, boolean) instead.
Create a temporary file in a given directory.
public File

Returns:

a File reference to the new temporary file.
createTempFile
(final Project
reference to the current Ant project.
project
,
String
file name prefix.
prefix
,
String
file extension; include the '.'.
suffix
,
final File
Directory to create the temporary file in; if not specified and project is not null then the value of the property ant.tmpdir is used if set; otherwise java.io.tmpdir is used.
parentDir
,
final boolean
whether to set the tempfile for deletion on normal VM exit.
deleteOnExit
,
final boolean
true if the file must actually be created. If false chances exist that a file with the same name is created in the time between invoking this method and the moment the file is actually created. If possible set to true.
createFile
)

Create a temporary file in a given directory.

public File

Returns:

a File reference to the new, nonexistent temporary file.
createTempFile
(String
file name prefix.
prefix
,
String
file extension; include the '.'.
suffix
,
File
Directory to create the temporary file in; java.io.tmpdir used if not specified.
parentDir
,
boolean
whether to set the tempfile for deletion on normal VM exit.
deleteOnExit
)

Deprecated since ant 1.7.1 use createTempFile(Project, String, String, File, boolean, boolean) instead.
Create a File object for a temporary file in a given directory.
public static void
delete(File
file to delete.
file
)

Delete the file with File#delete() if the argument is not null.

public String[]

Returns:

String[] {root, remaining path}.
dissect
(String
the path to dissect.
path
)

Dissect the specified absolute path.

public boolean

Returns:

true if the file are for the same file.
fileNameEquals
(File
the file whose name is to be compared.
f1
,
File
the other file whose name is to be compared.
f2
)

Compares two filenames.

public String

Returns:

the local file system path for the file.
fromURI
(String
the URI designating a file in the local filesystem.
uri
)

Constructs a file path from a file: URI.

public String

Returns:

the default file encoding.
getDefaultEncoding
()

Get the default encoding.

public long

Returns:

the difference, in milliseconds, which two file timestamps must have in order for the two files to be considered to have different timestamps.
getFileTimestampGranularity
()

Get the granularity of file timestamps.

public URL

Returns:

The FileURL value.
getFileURL
(File
the file whose URL representation is required.
file
)

Get the URL for a file taking into account # characters.

public static FileUtils

Returns:

an instance of FileUtils.
getFileUtils
()

Method to retrieve The FileUtils, which is shared by all users of this method.

public File

Returns:

the given file's parent, or null if the file does not have a parent.
getParentFile
(File
the file whose parent is required.
f
)

Deprecated since 1.7. Just use File#getParentFile directly.
This was originally an emulation of File#getParentFile for JDK 1.1, but it is now implemented using that method (Ant 1.6.3 onwards).
public static String

Returns:

String, never null
getPath
(List<String>
List of Strings to be concatenated as a path.
pathStack
)

Gets path from a List of Strings.

public static String

Returns:

String, never null
getPath
(final List<String>
List of Strings to be concatenated as a path.
pathStack
,
final char
char to be used as separator between names in path
separatorChar
)

Gets path from a List of Strings.

public static String[]

Returns:

Strings, never null
getPathStack
(String
to get names from
path
)

Gets all names of the path as an array of Strings.

public static String

Returns:

the relative path between the files
getRelativePath
(File
the File to calculate the path from
fromFile
,
File
the File to calculate the path to
toFile
)

Calculates the relative path between two files.

public boolean

Returns:

true if the file exists and the case of the actual file is not the case of the parameter
hasErrorInCase
(File
file to test
localFile
)

test whether a file or directory exists, with an error in the upper/lower case spelling of the name.

public static boolean

Returns:

true if the filename represents an absolute path.
isAbsolutePath
(String
the filename to be checked.
filename
)

Verifies that the specified filename represents an absolute path.

public static Optional<Boolean>

Returns:

Returns true if the filesystem corresponding to the passed path is case sensitive. Else returns false. If the case sensitivity cannot be determined for whatever reason, this method returns an Optional#empty()
isCaseSensitiveFileSystem
(final Path
The path whose filesystem case sensitivity needs to be checked
path
)

Tries to determine the case sensitivity of the filesystem corresponding to the path.

public static boolean

Returns:

true if the filename is relative to system context.
isContextRelativePath
(String
the filename to evaluate.
filename
)

On DOS and NetWare, the evaluation of certain file specifications is context-dependent.

public boolean

Returns:

true if path starts with leading; false otherwise.
isLeadingPath
(File
The leading path, must not be null, must be absolute.
leading
,
File
The path to check, must not be null, must be absolute.
path
)

Learn whether one path "leads" another.

public boolean

Returns:

true if path starts with leading; false otherwise.
isLeadingPath
(File
The leading path, must not be null, must be absolute.
leading
,
File
The path to check, must not be null, must be absolute.
path
,
boolean
whether symbolic links shall be resolved prior to comparing the paths.
resolveSymlinks
)

Learn whether one path "leads" another.

private boolean
public boolean

Returns:

true if the source is older than the dest after accounting for granularity.
isUpToDate
(File
source file (should be the older).
source
,
File
dest file (should be the newer).
dest
,
long
an offset added to the source time.
granularity
)

Returns true if the source is older than the dest.

public boolean

Returns:

true if the source is older than the dest, taking the granularity into account.
isUpToDate
(File
source file (should be the older).
source
,
File
dest file (should be the newer).
dest
)

Returns true if the source is older than the dest.

public boolean

Returns:

true if the dest file is considered up to date.
isUpToDate
(long
timestamp of source file.
sourceTime
,
long
timestamp of dest file.
destTime
,
long
os/filesys granularity.
granularity
)

Compare two timestamps for being up to date using the specified granularity.

public boolean

Returns:

true if the dest file is considered up to date.
isUpToDate
(long
timestamp of source file.
sourceTime
,
long
timestamp of dest file.
destTime
)

Compare two timestamps for being up to date using the current granularity.

public static FileUtils

Returns:

a new instance of FileUtils.
newFileUtils
()

Deprecated since 1.7. Use getFileUtils instead, FileUtils do not have state.
Factory method.
public static OutputStream

Returns:

a stream ready to write to the file
newOutputStream
(Path
the path of the file
path
,
boolean
whether to append to or a replace an existing file
append
)

Opens a new OutputStream for the given Path.

public File

Returns:

the normalized version of the path.
normalize
(final String
the path to be normalized.
path
)

"Normalize" the given absolute path.

public static String

Returns:

the contents read out of the given reader.
readFully
(Reader
the reader from which to read.
rdr
)

Read from reader till EOF.

public static String

Returns:

the contents read out of the given reader.
readFully
(Reader
the reader from which to read.
rdr
,
int
the buffer size to use when reading.
bufferSize
)

Read from reader till EOF.

public String

Returns:

path's normalized absolute if it doesn't start with leading; path's path with leading's path removed otherwise.
removeLeadingPath
(File
The leading path, must not be null, must be absolute.
leading
,
File
The path to remove from, must not be null, must be absolute.
path
)

Removes a leading path from a second path.

public void
rename(File
the file to move.
from
,
File
the new file name.
to
)

Renames a file, even if that involves crossing file system boundaries.

public void
rename(File
the file to move.
from
,
File
the new file name.
to
,
boolean
keep target file permissions
keepTargetFilePermissions
)

Renames a file, even if that involves crossing file system boundaries.

public File

Returns:

an absolute file.
resolveFile
(File
the "reference" file for relative paths. This instance must be an absolute file and must not contain "./" or "../" sequences (same for \ instead of /). If it is null, this call is equivalent to new java.io.File(filename).getAbsoluteFile().
file
,
String
a file name.
filename
)

Interpret the filename as a file relative to the given file unless the filename already represents an absolute filename.

public static String

Returns:

the string.
safeReadFully
(Reader
the input to read from.
reader
)

Safe read fully - do not return a null for an empty reader.

public void
setFileLastModified(File
the file whose modified time is to be set
file
,
long
the time to which the last modified time is to be set. if this is -1, the current time is used.
time
)

Calls File.setLastModified(long time).

public String

Returns:

the URI version of the local path.
toURI
(String
the path in the local file system.
path
)

Constructs a file: URI that represents the external form of the given pathname.

public String

Returns:

The absolute VMS path to f.
toVMSPath
(File
The File to get the VMS path for.
f
)

Returns a VMS String representation of a File object.

public static String

Returns:

the native version of the specified path or an empty string if the path is null or empty.
translatePath
(String
The path to be translated. May be null.
toProcess
)

Translate a path into its native (platform specific) format.

public boolean

Returns:

whether deletion was successful
tryHardToDelete
(File
File
f
)

Accommodate Windows bug encountered in both Sun and IBM JDKs.

public boolean

Returns:

whether deletion was successful
tryHardToDelete
(File
File
f
,
boolean
boolean
runGC
)

If delete does not work, call System.gc() if asked to, wait a little and try again.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait