org.apache.tools.ant.util
public class FileUtils extends Object
Field Summary | |
---|---|
static long | FAT_FILE_TIMESTAMP_GRANULARITY
The granularity of timestamps under FAT. |
static long | UNIX_FILE_TIMESTAMP_GRANULARITY
The granularity of timestamps under Unix. |
Constructor Summary | |
---|---|
protected | FileUtils()
Empty constructor. |
Method Summary | |
---|---|
static void | close(Writer device)
Close a Writer without throwing any exception if something went wrong.
|
static void | close(Reader device)
Close a stream without throwing any exception if something went wrong.
|
static void | close(OutputStream device)
Close a stream without throwing any exception if something went wrong.
|
static void | close(InputStream device)
Close a stream without throwing any exception if something went wrong.
|
boolean | contentEquals(File f1, File f2)
Compares the contents of two files.
|
boolean | contentEquals(File f1, File f2, boolean textfile)
Compares the contents of two files.
|
void | copyFile(String sourceFile, String destFile)
Convenience method to copy a file from a source to a destination.
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters)
Convenience method to copy a file from a source to a destination
specifying if token filtering must be used.
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean 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.
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean 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 .
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified, String 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 .
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String encoding, Project 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 .
|
void | copyFile(String sourceFile, String destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project 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 .
|
void | copyFile(File sourceFile, File destFile)
Convenience method to copy a file from a source to a destination.
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters)
Convenience method to copy a file from a source to a destination
specifying if token filtering must be used.
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean 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.
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean overwrite, boolean 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 .
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified, String 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.
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String encoding, Project 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 .
|
void | copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project 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 .
|
boolean | createNewFile(File 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).
|
boolean | createNewFile(File f, boolean mkdirs)
Create a new file, optionally creating parent directories.
|
File | createTempFile(String prefix, String suffix, File parentDir)
Create a temporary file in a given directory.
|
static void | delete(File file)
Delete the file with {@link File#delete()} if the argument is not null.
|
boolean | fileNameEquals(File f1, File f2)
Compares two filenames.
|
String | fromURI(String uri)
Constructs a file path from a file: URI.
|
long | getFileTimestampGranularity()
Get the granularity of file timestamps.
|
URL | getFileURL(File file)
Get the URL for a file taking into account # characters.
|
static FileUtils | getFileUtils()
Method to retrieve The FileUtils, which is shared by all users of this
method. |
File | getParentFile(File f)
This was originally an emulation of {@link File#getParentFile} for JDK 1.1,
but it is now implemented using that method (Ant 1.6.3 onwards). |
static boolean | isAbsolutePath(String filename)
Verifies that the specified filename represents an absolute path. |
boolean | isSymbolicLink(File parent, String name)
Checks whether a given file is a symbolic link.
|
boolean | isUpToDate(File source, File dest, long granularity)
Returns true if the source is older than the dest.
|
boolean | isUpToDate(File source, File dest)
Returns true if the source is older than the dest. |
boolean | isUpToDate(long sourceTime, long destTime, long granularity)
Compare two timestamps for being up to date using
the specified granularity.
|
boolean | isUpToDate(long sourceTime, long destTime)
Compare two timestamps for being up to date using the
current granularity.
|
static FileUtils | newFileUtils()
Factory method.
|
File | normalize(String path)
"Normalize" the given absolute path.
|
static String | readFully(Reader rdr)
Read from reader till EOF. |
static String | readFully(Reader rdr, int bufferSize)
Read from reader till EOF.
|
String | removeLeadingPath(File leading, File path)
Removes a leading path from a second path.
|
void | rename(File from, File to)
Renames a file, even if that involves crossing file system boundaries.
|
File | resolveFile(File file, String filename)
Interpret the filename as a file relative to the given file
unless the filename already represents an absolute filename.
|
void | setFileLastModified(File file, long time)
Calls File.setLastModified(long time). |
String | toURI(String path)
Constructs a file: URI that represents the
external form of the given pathname.
|
String | toVMSPath(File f)
Returns a VMS String representation of a File object.
|
Parameters: device output writer, can be null.
Parameters: device Reader, can be null.
Parameters: device stream, can be null.
Parameters: device stream, can be null.
Parameters: f1 the file whose content is to be compared. f2 the other file whose content is to be compared.
Returns: true if the content of the files is the same.
Throws: IOException if the files cannot be read.
Parameters: f1 the file whose content is to be compared. f2 the other file whose content is to be compared. textfile true if the file is to be treated as a text file and differences in kind of line break are to be ignored.
Returns: true if the content of the files is the same.
Throws: IOException if the files cannot be read.
Since: Ant 1.6.3
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
.
Throws: IOException if the copying fails.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy.
Throws: IOException if the copying fails.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists.
Throws: IOException if the copying fails.
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file.
Throws: IOException if the copying fails.
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. encoding the encoding used to read and write the files.
Throws: IOException if the copying fails.
Since: Ant 1.5
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. filterChains filterChains to apply during the copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. encoding the encoding used to read and write the files. project the project instance.
Throws: IOException if the copying fails.
Since: Ant 1.5
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile Name of file to copy from.
Must not be null
. destFile Name of file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. filterChains filterChains to apply during the copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. inputEncoding the encoding used to read the files. outputEncoding the encoding used to write the files. project the project instance.
Throws: IOException if the copying fails.
Since: Ant 1.6
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
.
Throws: IOException if the copying fails.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy.
Throws: IOException if the copying fails.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists.
Throws: IOException if the copying fails.
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file.
Throws: IOException if the copying fails.
destFile
file should be made
equal to the last modified time of sourceFile
and
which character encoding to assume.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. encoding the encoding used to read and write the files.
Throws: IOException if the copying fails.
Since: Ant 1.5
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. filterChains filterChains to apply during the copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. encoding the encoding used to read and write the files. project the project instance.
Throws: IOException if the copying fails.
Since: Ant 1.5
destFile
file should be made equal
to the last modified time of sourceFile
.
Parameters: sourceFile the file to copy from.
Must not be null
. destFile the file to copy to.
Must not be null
. filters the collection of filters to apply to this copy. filterChains filterChains to apply during the copy. overwrite Whether or not the destination file should be
overwritten if it already exists. preserveLastModified Whether or not the last modified time of
the resulting file should be set to that
of the source file. inputEncoding the encoding used to read the files. outputEncoding the encoding used to write the files. project the project instance.
Throws: IOException if the copying fails.
Since: Ant 1.6
This method has historically not guaranteed that the operation was atomic. In its current implementation it is.
Parameters: f the file to be created.
Returns: true if the file did not exist already.
Throws: IOException on error.
Since: Ant 1.5
Parameters: f the file to be created. mkdirs boolean
whether to create parent directories.
Returns: true if the file did not exist already.
Throws: IOException on error.
Since: Ant 1.6.3
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 from File.createTempFile() 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.
Parameters: prefix prefix before the random number. suffix file extension; include the '.'. parentDir Directory to create the temporary file in; java.io.tmpdir used if not specified.
Returns: a File reference to the new temporary file.
Since: Ant 1.5
Parameters: file file to delete.
Unlike java.io.File#equals this method will try to compare the absolute paths and "normalize" the filenames before comparing them.
Parameters: f1 the file whose name is to be compared. f2 the other file whose name is to be compared.
Returns: true if the file are for the same file.
Since: Ant 1.5.3
file:
URI.
Will be an absolute path if the given URI is absolute.
Swallows '%' that are not followed by two characters, doesn't deal with non-ASCII characters.
Parameters: uri the URI designating a file in the local filesystem.
Returns: the local file system path for the file.
Since: Ant 1.6
Returns: the difference, in milliseconds, which two file timestamps must have in order for the two files to be given a creation order.
Parameters: file the file whose URL representation is required.
Returns: The FileURL value.
Throws: MalformedURLException if the URL representation cannot be formed.
Returns: an instance of FileUtils.
Since: Ant 1.6.3
Parameters: f the file whose parent is required.
Returns: the given file's parent, or null if the file does not have a parent.
Since: 1.10
Parameters: filename the filename to be checked.
Returns: true if the filename represents an absolute path.
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: parent the parent directory of the file to test name the name of the file to test.
Returns: true if the file is a symbolic link.
Throws: IOException on error.
Since: Ant 1.5
Parameters: source source file (should be the older). dest dest file (should be the newer). granularity an offset added to the source time.
Returns: true if the source is older than the dest after accounting for granularity.
Since: Ant 1.6.3
Parameters: source source file (should be the older). dest dest file (should be the newer).
Returns: true if the source is older than the dest, taking the granularity into account.
Since: Ant 1.6.3
Parameters: sourceTime timestamp of source file. destTime timestamp of dest file. granularity os/filesys granularity.
Returns: true if the dest file is considered up to date.
Parameters: sourceTime timestamp of source file. destTime timestamp of dest file.
Returns: true if the dest file is considered up to date.
Returns: a new instance of FileUtils.
This includes:
File#getCanonicalPath()
this method
specifically does not resolve symbolic links.
Parameters: path the path to be normalized.
Returns: the normalized version of the path.
Throws: java.lang.NullPointerException if the file path is equal to null.
Parameters: rdr the reader from which to read.
Returns: the contents read out of the given reader.
Throws: IOException if the contents could not be read out from the reader.
Parameters: rdr the reader from which to read. bufferSize the buffer size to use when reading.
Returns: the contents read out of the given reader.
Throws: IOException if the contents could not be read out from the reader.
Parameters: leading The leading path, must not be null, must be absolute. path The path to remove from, must not be null, must be absolute.
Returns: path's normalized absolute if it doesn't start with leading; path's path with leading's path removed otherwise.
Since: Ant 1.5
This will remove to
(if it exists), ensure that
to
's parent directory exists and move
from
, which involves deleting from
as
well.
Parameters: from the file to move. to the new file name.
Throws: IOException if anything bad happens during this
process. Note that to
may have been deleted
already when this happens.
Since: Ant 1.6
Parameters: 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)
.
filename a file name.
Returns: an absolute file that doesn't contain "./" or "../" sequences and uses the correct separator for the current platform.
Parameters: file the file whose modified time is to be set time the time to which the last modified time is to be set. if this is -1, the current time is used.
file:
URI that represents the
external form of the given pathname.
Will be an absolute URI if the given path is absolute.
This code doesn't handle non-ASCII characters properly.
Parameters: path the path in the local file system.
Returns: the URI version of the local path.
Since: Ant 1.6
File
object.
This is useful since the JVM by default internally converts VMS paths
to Unix style.
The returned String is always an absolute path.
Parameters: f The File
to get the VMS path for.
Returns: The absolute VMS path to f
.