Save This Page
Home » apache-ant-1.7.1-src » org.apache.tools » ant » util » [javadoc | source]
org.apache.tools.ant.util
public class: FileUtils [javadoc | source]
java.lang.Object
   org.apache.tools.ant.util.FileUtils
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
static final  int BUF_SIZE     
public static final  long FAT_FILE_TIMESTAMP_GRANULARITY    The granularity of timestamps under FAT. 
public static final  long UNIX_FILE_TIMESTAMP_GRANULARITY    The granularity of timestamps under Unix. 
public static final  long NTFS_FILE_TIMESTAMP_GRANULARITY    The granularity of timestamps under the NT File System. NTFS has a granularity of 100 nanoseconds, which is less than 1 millisecond, so we round this up to 1 millisecond. 
Constructor:
 protected FileUtils() 
Method from org.apache.tools.ant.util.FileUtils Summary:
close,   close,   close,   close,   contentEquals,   contentEquals,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   copyFile,   createNewFile,   createNewFile,   createTempFile,   createTempFile,   createTempFile,   delete,   dissect,   fileNameEquals,   fromURI,   getDefaultEncoding,   getFileTimestampGranularity,   getFileURL,   getFileUtils,   getParentFile,   getPath,   getPath,   getPathStack,   getRelativePath,   hasErrorInCase,   isAbsolutePath,   isContextRelativePath,   isLeadingPath,   isSymbolicLink,   isUpToDate,   isUpToDate,   isUpToDate,   isUpToDate,   newFileUtils,   normalize,   readFully,   readFully,   removeLeadingPath,   rename,   resolveFile,   safeReadFully,   setFileLastModified,   toURI,   toVMSPath,   translatePath
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.tools.ant.util.FileUtils Detail:
 public static  void close(Writer device) 
    Close a Writer without throwing any exception if something went wrong. Do not attempt to close it if the argument is null.
 public static  void close(Reader device) 
    Close a Reader without throwing any exception if something went wrong. Do not attempt to close it if the argument is null.
 public static  void close(OutputStream device) 
    Close a stream without throwing any exception if something went wrong. Do not attempt to close it if the argument is null.
 public static  void close(InputStream device) 
    Close a stream without throwing any exception if something went wrong. Do not attempt to close it if the argument is null.
 public boolean contentEquals(File f1,
    File f2) throws IOException 
    Compares the contents of two files.
 public boolean contentEquals(File f1,
    File f2,
    boolean textfile) throws IOException 
    Compares the contents of two files.
 public  void copyFile(String sourceFile,
    String destFile) throws IOException 
    Convenience method to copy a file from a source to a destination. No filtering is performed.
 public  void copyFile(File sourceFile,
    File destFile) throws IOException 
    Convenience method to copy a file from a source to a destination. No filtering is performed.
 public  void copyFile(String sourceFile,
    String destFile,
    FilterSetCollection filters) throws IOException 
    Convenience method to copy a file from a source to a destination specifying if token filtering must be used.
 public  void copyFile(File sourceFile,
    File destFile,
    FilterSetCollection filters) throws IOException 
    Convenience method to copy a file from a source to a destination specifying if token filtering must be used.
 public  void copyFile(String sourceFile,
    String destFile,
    FilterSetCollection filters,
    boolean overwrite) throws IOException 
    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 sourceFile,
    File destFile,
    FilterSetCollection filters,
    boolean overwrite) throws IOException 
    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 sourceFile,
    String destFile,
    FilterSetCollection filters,
    boolean overwrite,
    boolean preserveLastModified) throws IOException 
    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 sourceFile,
    File destFile,
    FilterSetCollection filters,
    boolean overwrite,
    boolean preserveLastModified) throws IOException 
    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 sourceFile,
    String destFile,
    FilterSetCollection filters,
    boolean overwrite,
    boolean preserveLastModified,
    String encoding) throws IOException 
    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 sourceFile,
    File destFile,
    FilterSetCollection filters,
    boolean overwrite,
    boolean preserveLastModified,
    String encoding) throws IOException 
    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(String sourceFile,
    String destFile,
    FilterSetCollection filters,
    Vector filterChains,
    boolean overwrite,
    boolean preserveLastModified,
    String encoding,
    Project project) throws IOException 
    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 sourceFile,
    File destFile,
    FilterSetCollection filters,
    Vector filterChains,
    boolean overwrite,
    boolean preserveLastModified,
    String encoding,
    Project project) throws IOException 
    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 sourceFile,
    String destFile,
    FilterSetCollection filters,
    Vector filterChains,
    boolean overwrite,
    boolean preserveLastModified,
    String inputEncoding,
    String outputEncoding,
    Project project) throws IOException 
    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 sourceFile,
    File destFile,
    FilterSetCollection filters,
    Vector filterChains,
    boolean overwrite,
    boolean preserveLastModified,
    String inputEncoding,
    String outputEncoding,
    Project project) throws IOException 
    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 createNewFile(File f) throws IOException 
    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).

    This method has historically not guaranteed that the operation was atomic. In its current implementation it is.

 public boolean createNewFile(File f,
    boolean mkdirs) throws IOException 
    Create a new file, optionally creating parent directories.
 public File createTempFile(String prefix,
    String suffix,
    File parentDir) 
Deprecated! since - Ant 1.7.1

    Create a File object for a temporary file in a given directory. Without actually creating the file.

    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.

 public File createTempFile(String prefix,
    String suffix,
    File parentDir,
    boolean deleteOnExit) 
Deprecated! since - Ant 1.7.1

    Create a File object for a temporary file in a given directory. Without actually creating the file.

    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.

 public File createTempFile(String prefix,
    String suffix,
    File parentDir,
    boolean deleteOnExit,
    boolean createFile) 
    Create a temporary file in a given directory.

    The file denoted by the returned abstract pathname did not exist before this method was invoked, any subsequent invocation of this method will yield a different file name.

 public static  void delete(File file) 
    Delete the file with File#delete() if the argument is not null. Do nothing on a null argument.
 public String[] dissect(String path) 
    Dissect the specified absolute path.
 public boolean fileNameEquals(File f1,
    File f2) 
    Compares two filenames.

    Unlike java.io.File#equals this method will try to compare the absolute paths and "normalize" the filenames before comparing them.

 public String fromURI(String uri) 
    Constructs a file path from a 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.

 public String getDefaultEncoding() 
    Get the default encoding. This is done by opening an InputStreamReader on a dummy InputStream and getting the encoding. Could use System.getProperty("file.encoding"), but cannot see where this is documented.
 public long getFileTimestampGranularity() 
    Get the granularity of file timestamps. The choice is made based on OS, which is incorrect--it should really be by filesystem. We do not have an easy way to probe for file systems, however, so this heuristic gives us a decent default.
 public URL getFileURL(File file) throws MalformedURLException 
    Get the URL for a file taking into account # characters.
 public static FileUtils getFileUtils() 
    Method to retrieve The FileUtils, which is shared by all users of this method.
 public File getParentFile(File 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 getPath(List pathStack) 
    Gets path from a List of Strings.
 public static String getPath(List pathStack,
    char separatorChar) 
    Gets path from a List of Strings.
 public static String[] getPathStack(String path) 
    Gets all names of the path as an array of Strings.
 public static String getRelativePath(File fromFile,
    File toFile) throws Exception 
    Calculates the relative path between two files.

    Implementation note:
    This function may throw an IOException if an I/O error occurs because its use of the canonical pathname may require filesystem queries.

 public boolean hasErrorInCase(File localFile) 
    test whether a file or directory exists, with an error in the upper/lower case spelling of the name. Using this method is only interesting on case insensitive file systems (Windows).
    It will return true only if 3 conditions are met :
    • operating system is case insensitive
    • file exists
    • actual name from directory reading is different from the supplied argument

    the purpose is to identify files or directories on case-insensitive filesystems whose case is not what is expected.
    Possibly to rename them afterwards to the desired upper/lowercase combination.
 public static boolean isAbsolutePath(String filename) 
    Verifies that the specified filename represents an absolute path. Differs from new java.io.File("filename").isAbsolute() in that a path beginning with a double file separator--signifying a Windows UNC--must at minimum match "\\a\b" to be considered an absolute path.
 public static boolean isContextRelativePath(String filename) 
    On DOS and NetWare, the evaluation of certain file specifications is context-dependent. These are filenames beginning with a single separator (relative to current root directory) and filenames with a drive specification and no intervening separator (relative to current directory of the specified root).
 public boolean isLeadingPath(File leading,
    File path) 
    Learn whether one path "leads" another.
 public boolean isSymbolicLink(File parent,
    String name) throws IOException 
    Checks whether a given file is a symbolic link.

    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.

 public boolean isUpToDate(File source,
    File dest) 
    Returns true if the source is older than the dest.
 public boolean isUpToDate(long sourceTime,
    long destTime) 
    Compare two timestamps for being up to date using the current granularity.
 public boolean isUpToDate(File source,
    File dest,
    long granularity) 
    Returns true if the source is older than the dest. If the dest file does not exist, then the test returns false; it is implicitly not up do date.
 public boolean isUpToDate(long sourceTime,
    long destTime,
    long granularity) 
    Compare two timestamps for being up to date using the specified granularity.
 public static FileUtils newFileUtils() 
Deprecated! since - 1.7. Use getFileUtils instead, FileUtils do not have state.

    Factory method.
 public File normalize(String path) 
    "Normalize" the given absolute path.

    This includes:

    • Uppercase the drive letter if there is one.
    • Remove redundant slashes after the drive spec.
    • Resolve all ./, .\, ../ and ..\ sequences.
    • DOS style paths that start with a drive letter will have \ as the separator.
    Unlike File#getCanonicalPath() this method specifically does not resolve symbolic links.
 public static String readFully(Reader rdr) throws IOException 
    Read from reader till EOF.
 public static String readFully(Reader rdr,
    int bufferSize) throws IOException 
    Read from reader till EOF.
 public String removeLeadingPath(File leading,
    File path) 
    Removes a leading path from a second path.
 public  void rename(File from,
    File to) throws IOException 
    Renames a file, even if that involves crossing file system boundaries.

    This will remove to (if it exists), ensure that to's parent directory exists and move from, which involves deleting from as well.

 public 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. Differs from new File(file, filename) in that the resulting File's path will always be a normalized, absolute pathname. Also, if it is determined that filename is context-relative, file will be discarded and the reference will be resolved using available context/state information about the filesystem.
 public static String safeReadFully(Reader reader) throws IOException 
    Safe read fully - do not return a null for an empty reader.
 public  void setFileLastModified(File file,
    long time) 
    Calls File.setLastModified(long time). Originally written to to dynamically bind to that call on Java1.2+.
 public String toURI(String path) 
    Constructs a file: URI that represents the external form of the given pathname.

    Will be an absolute URI if the given path is absolute.

    This code encodes non ASCII characters too.

    The coding of the output is the same as what File.toURI().toASCIIString() produces

    See dt-sysid which makes some mention of how characters not supported by URI Reference syntax should be escaped.
 public String toVMSPath(File f) 
    Returns a VMS String representation of a 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.
 public static String translatePath(String toProcess) 
    Translate a path into its native (platform specific) format.

    This method uses PathTokenizer to separate the input path into its components. This handles DOS style paths in a relatively sensible way. The file separators are then converted to their platform specific versions.