⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutils.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * last modified time of <code>destFile</code> file should be made equal     * to the last modified time of <code>sourceFile</code>.     *     * @param sourceFile the file to copy from.     *                   Must not be <code>null</code>.     * @param destFile the file to copy to.     *                 Must not be <code>null</code>.     * @param filters the collection of filters to apply to this copy.     * @param overwrite Whether or not the destination file should be     *                  overwritten if it already exists.     * @param 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.     */    public void copyFile(File sourceFile, File destFile, FilterSetCollection filters,                         boolean overwrite, boolean preserveLastModified)        throws IOException {        copyFile(sourceFile, destFile, filters, overwrite,                 preserveLastModified, null);    }    /**     * 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 <code>destFile</code> file should be made     * equal to the last modified time of <code>sourceFile</code> and     * which character encoding to assume.     *     * @param sourceFile the file to copy from.     *                   Must not be <code>null</code>.     * @param destFile the file to copy to.     *                 Must not be <code>null</code>.     * @param filters the collection of filters to apply to this copy.     * @param overwrite Whether or not the destination file should be     *                  overwritten if it already exists.     * @param preserveLastModified Whether or not the last modified time of     *                             the resulting file should be set to that     *                             of the source file.     * @param encoding the encoding used to read and write the files.     *     * @throws IOException if the copying fails.     *     * @since Ant 1.5     */    public void copyFile(File sourceFile, File destFile,                         FilterSetCollection filters, boolean overwrite,                         boolean preserveLastModified, String encoding)        throws IOException {        copyFile(sourceFile, destFile, filters, null, overwrite,                 preserveLastModified, encoding, null);    }    /**     * 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     * <code>destFile</code> file should be made equal     * to the last modified time of <code>sourceFile</code>.     *     * @param sourceFile the file to copy from.     *                   Must not be <code>null</code>.     * @param destFile the file to copy to.     *                 Must not be <code>null</code>.     * @param filters the collection of filters to apply to this copy.     * @param filterChains filterChains to apply during the copy.     * @param overwrite Whether or not the destination file should be     *                  overwritten if it already exists.     * @param preserveLastModified Whether or not the last modified time of     *                             the resulting file should be set to that     *                             of the source file.     * @param encoding the encoding used to read and write the files.     * @param project the project instance.     *     * @throws IOException if the copying fails.     *     * @since Ant 1.5     */    public void copyFile(File sourceFile, File destFile,                         FilterSetCollection filters, Vector filterChains,                         boolean overwrite, boolean preserveLastModified,                         String encoding, Project project)        throws IOException {        copyFile(sourceFile, destFile, filters, filterChains,                 overwrite, preserveLastModified, encoding, encoding, 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     * <code>destFile</code> file should be made equal     * to the last modified time of <code>sourceFile</code>.     *     * @param sourceFile the file to copy from.     *                   Must not be <code>null</code>.     * @param destFile the file to copy to.     *                 Must not be <code>null</code>.     * @param filters the collection of filters to apply to this copy.     * @param filterChains filterChains to apply during the copy.     * @param overwrite Whether or not the destination file should be     *                  overwritten if it already exists.     * @param preserveLastModified Whether or not the last modified time of     *                             the resulting file should be set to that     *                             of the source file.     * @param inputEncoding the encoding used to read the files.     * @param outputEncoding the encoding used to write the files.     * @param project the project instance.     *     *     * @throws IOException if the copying fails.     *     * @since Ant 1.6     */    public void copyFile(File sourceFile, File destFile,                         FilterSetCollection filters, Vector filterChains,                         boolean overwrite, boolean preserveLastModified,                         String inputEncoding, String outputEncoding,                         Project project)        throws IOException {        ResourceUtils.copyResource(            new FileResource(sourceFile), new FileResource(destFile),            filters, filterChains, overwrite, preserveLastModified,            inputEncoding, outputEncoding, project);    }    // CheckStyle:ParameterNumberCheck ON    /**     * Calls File.setLastModified(long time). Originally written to     * to dynamically bind to that call on Java1.2+.     *     * @param file the file whose modified time is to be set     * @param time the time to which the last modified time is to be set.     *             if this is -1, the current time is used.     */    public void setFileLastModified(File file, long time) {        ResourceUtils.setLastModified(new FileResource(file), time);    }    /**     * Interpret the filename as a file relative to the given file     * unless the filename already represents an absolute filename.     * Differs from <code>new File(file, filename)</code> in that     * the resulting File's path will always be a normalized,     * absolute pathname.  Also, if it is determined that     * <code>filename</code> is context-relative, <code>file</code>     * will be discarded and the reference will be resolved using     * available context/state information about the filesystem.     *     * @param file the "reference" file for relative paths. This     * instance must be an absolute file and must not contain     * &quot;./&quot; or &quot;../&quot; sequences (same for \ instead     * of /).  If it is null, this call is equivalent to     * <code>new java.io.File(filename).getAbsoluteFile()</code>.     *     * @param filename a file name.     *     * @return an absolute file.     * @throws java.lang.NullPointerException if filename is null.     */    public File resolveFile(File file, String filename) {        if (!isAbsolutePath(filename)) {            char sep = File.separatorChar;            filename = filename.replace('/', sep).replace('\\', sep);            if (isContextRelativePath(filename)) {                file = null;                // on cygwin, our current directory can be a UNC;                // assume user.dir is absolute or all hell breaks loose...                String udir = System.getProperty("user.dir");                if (filename.charAt(0) == sep && udir.charAt(0) == sep) {                    filename = dissect(udir)[0] + filename.substring(1);                }            }            filename = new File(file, filename).getAbsolutePath();        }        return normalize(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).     * @param filename the filename to evaluate.     * @return true if the filename is relative to system context.     * @throws java.lang.NullPointerException if filename is null.     * @since Ant 1.7     */    public static boolean isContextRelativePath(String filename) {        if (!(onDos || onNetWare) || filename.length() == 0) {            return false;        }        char sep = File.separatorChar;        filename = filename.replace('/', sep).replace('\\', sep);        char c = filename.charAt(0);        int len = filename.length();        return (c == sep && (len == 1 || filename.charAt(1) != sep))            || (Character.isLetter(c) && len > 1            && filename.indexOf(':') == 1            && (len == 2 || filename.charAt(2) != sep));    }    /**     * 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.     * @param filename the filename to be checked.     * @return true if the filename represents an absolute path.     * @throws java.lang.NullPointerException if filename is null.     * @since Ant 1.6.3     */    public static boolean isAbsolutePath(String filename) {        int len = filename.length();        if (len == 0) {            return false;        }        char sep = File.separatorChar;        filename = filename.replace('/', sep).replace('\\', sep);        char c = filename.charAt(0);        if (!(onDos || onNetWare)) {            return (c == sep);        }        if (c == sep) {            if (!(onDos && len > 4 && filename.charAt(1) == sep)) {                return false;            }            int nextsep = filename.indexOf(sep, 2);            return nextsep > 2 && nextsep + 1 < len;        }        int colon = filename.indexOf(':');        return (Character.isLetter(c) && colon == 1            && filename.length() > 2 && filename.charAt(2) == sep)            || (onNetWare && colon > 0);    }    /**     * Translate a path into its native (platform specific) format.     * <p>     * 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.     *     * @param toProcess The path to be translated.     *                  May be <code>null</code>.     *     * @return the native version of the specified path or     *         an empty string if the path is <code>null</code> or empty.     *     * @since ant 1.7     * @see PathTokenizer     */    public static String translatePath(String toProcess) {        if (toProcess == null || toProcess.length() == 0) {            return "";        }        StringBuffer path = new StringBuffer(toProcess.length() + 50);        PathTokenizer tokenizer = new PathTokenizer(toProcess);        while (tokenizer.hasMoreTokens()) {            String pathComponent = tokenizer.nextToken();            pathComponent = pathComponent.replace('/', File.separatorChar);            pathComponent = pathComponent.replace('\\', File.separatorChar);            if (path.length() != 0) {                path.append(File.pathSeparatorChar);            }            path.append(pathComponent);        }        return path.toString();    }    /**     * &quot;Normalize&quot; the given absolute path.     *     * <p>This includes:     * <ul>     *   <li>Uppercase the drive letter if there is one.</li>     *   <li>Remove redundant slashes after the drive spec.</li>     *   <li>Resolve all ./, .\, ../ and ..\ sequences.</li>     *   <li>DOS style paths that start with a drive letter will have     *     \ as the separator.</li>     * </ul>     * Unlike {@link File#getCanonicalPath()} this method     * specifically does not resolve symbolic links.     *     * @param path the path to be normalized.     * @return the normalized version of the path.     *     * @throws java.lang.NullPointerException if path is null.     */    public File normalize(final String path) {        Stack s = new Stack();        String[] dissect = dissect(path);        s.push(dissect[0]);        StringTokenizer tok = new StringTokenizer(dissect[1], File.separator);        while (tok.hasMoreTokens()) {            String thisToken = tok.nextToken();            if (".".equals(thisToken)) {                continue;            } else if ("..".equals(thisToken)) {                if (s.size() < 2) {                    // Cannot resolve it, so skip it.                    return new File(path);                }                s.pop();            } else { // plain component                s.push(thisToken);            }        }        StringBuffer sb = new StringBuffer();        for (int i = 0; i < s.size(); i++) {            if (i > 1) {                // not before the filesystem root and not after it, since root                // already contains one                sb.append(File.separatorChar);            }            sb.append(s.elementAt(i));        }        return new File(sb.toString());    }    /**     * Dissect the specified absolute path.     * @param path the path to dissect.     * @return String[] {root, remaining path}.     * @throws java.lang.NullPointerException if path is null.     * @since Ant 1.7     */    public String[] dissect(String path) {        char sep = File.separatorChar;        path = path.replace('/', sep).replace('\\', sep);        // make sure we are dealing with an absolute path        if (!isAbsolutePath(path)) {            throw new BuildException(path + " is not an absolute path");        }        String root = null;        int colon = path.indexOf(':');        if (colon > 0 && (onDos || onNetWare)) {            int next = colon + 1;            root = path.substring(0, next);            char[] ca = path.toCharArray();            root += sep;            //remove the initial separator; the root has it.            next = (ca[next] == sep) ? next + 1 : next;            StringBuffer sbPath = new StringBuffer();            // Eliminate consecutive slashes after the drive spec:            for (int i = next; i < ca.length; i++) {                if (ca[i] != sep || ca[i - 1] != sep) {                    sbPath.append(ca[i]);                }            }            path = sbPath.toString();        } else if (path.length() > 1 && path.charAt(1) == sep) {            // UNC drive            int nextsep = path.indexOf(sep, 2);            nextsep = path.indexOf(sep, nextsep + 1);            root = (nextsep > 2) ? path.substring(0, nextsep + 1) : path;            path = path.substring(root.length());        } else {            root = File.separator;            path = path.substring(1);        }        return new String[] {root, path};    }    /**     * Returns a VMS String representation of a <code>File</code> object.     * This is useful since the JVM by default internally converts VMS paths     * to Unix style.     * The returned String is always an absolute path.     *     * @param f The <code>File</code> to get the VMS path for.     * @return The absolute VMS path to <code>f</code>.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -