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

📄 fileutils.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * &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;            }            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 && (ON_DOS || ON_NETWARE)) {            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>.     */    public String toVMSPath(File f) {        // format: "DEVICE:[DIR.SUBDIR]FILE"        String osPath;        String path = normalize(f.getAbsolutePath()).getPath();        String name = f.getName();        boolean isAbsolute = path.charAt(0) == File.separatorChar;        // treat directories specified using .DIR syntax as files        // CheckStyle:MagicNumber OFF        boolean isDirectory = f.isDirectory()                && !name.regionMatches(true, name.length() - 4, ".DIR", 0, 4);        // CheckStyle:MagicNumber ON        String device = null;        StringBuffer directory = null;        String file = null;        int index = 0;        if (isAbsolute) {            index = path.indexOf(File.separatorChar, 1);            if (index == -1) {                return path.substring(1) + ":[000000]";            }            device = path.substring(1, index++);        }        if (isDirectory) {            directory = new StringBuffer(path.substring(index).replace(File.separatorChar, '.'));        } else {            int dirEnd = path.lastIndexOf(File.separatorChar, path.length());            if (dirEnd == -1 || dirEnd < index) {                file = path.substring(index);            } else {                directory = new StringBuffer(path.substring(index, dirEnd).                                             replace(File.separatorChar, '.'));                index = dirEnd + 1;                if (path.length() > index) {                    file = path.substring(index);                }            }        }        if (!isAbsolute && directory != null) {            directory.insert(0, '.');        }        osPath = ((device != null) ? device + ":" : "")                + ((directory != null) ? "[" + directory + "]" : "")                + ((file != null) ? file : "");        return osPath;    }    /**     * Create a File object for a temporary file in a given directory. Without     * actually creating the file.     *     * <p>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.</p>     * <p>     * The filename is prefixNNNNNsuffix where NNNN is a random number.     * </p>     *     * @param prefix prefix before the random number.     * @param suffix file extension; include the '.'.     * @param parentDir Directory to create the temporary file in;     * java.io.tmpdir used if not specified.     *     * @return a File reference to the new, nonexistent temporary file.     * @deprecated since Ant 1.7.1     * @since Ant 1.7     */    public File createTempFile(String prefix, String suffix, File parentDir) {        return createTempFile(prefix, suffix, parentDir, false);    }    /**     * Create a temporary file in a given directory.     *     * <p>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.</p>     *     * @param prefix prefix before the random number.     * @param suffix file extension; include the '.'.     * @param parentDir Directory to create the temporary file in;     * java.io.tmpdir used if not specified.     * @param deleteOnExit whether to set the tempfile for deletion on     *        normal VM exit.     * @param createFile 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.     * @return a File reference to the new temporary file.     * @since Ant 1.7.1     */    public File createTempFile(String prefix, String suffix, File parentDir,                               boolean deleteOnExit, boolean createFile) {        File result = null;        String parent = (parentDir == null)                ? System.getProperty("java.io.tmpdir")                : parentDir.getPath();        if (createFile) {            try {                result = File.createTempFile(prefix, suffix, new File(parent));            } catch (IOException e) {                throw new BuildException("Could not create tempfile in "                        + parent, e);            }        } else {            DecimalFormat fmt = new DecimalFormat("#####");            synchronized (rand) {                do {                    result = new File(parent, prefix                            + fmt.format(Math.abs(rand.nextInt())) + suffix);                } while (result.exists());            }        }        if (deleteOnExit) {            result.deleteOnExit();        }        return result;    }    /**     * Create a File object for a temporary file in a given directory. Without     * actually creating the file.     *     * <p>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.</p>     * <p>     * The filename is prefixNNNNNsuffix where NNNN is a random number.     * </p>     *     * @param prefix prefix before the random number.     * @param suffix file extension; include the '.'.     * @param parentDir Directory to create the temporary file in;     * java.io.tmpdir used if not specified.     * @param deleteOnExit whether to set the tempfile for deletion on     *        normal VM exit.     *     * @return a File reference to the new, nonexistent temporary file.     * @deprecated since Ant 1.7.1     * @since Ant 1.7     */    public File createTempFile(String prefix, String suffix,            File parentDir, boolean deleteOnExit) {        return createTempFile(prefix, suffix, parentDir, deleteOnExit, false);    }    /**     * Compares the contents of two files.     *     * @param f1 the file whose content is to be compared.     * @param f2 the other file whose content is to be compared.     *     * @return true if the content of the files is the same.     *     * @throws IOException if the files cannot be read.     */    public boolean contentEquals(File f1, File f2) throws IOException {        return contentEquals(f1, f2, false);    }    /**     * Compares the contents of two files.     *     * @param f1 the file whose content is to be compared.     * @param f2 the other file whose content is to be compared.     * @param textfile true if the file is to be treated as a text file and     *        differences in kind of line break are to be ignored.     *     * @return true if the content of the files is the same.     *     * @throws IOException if the files cannot be read.     * @since Ant 1.6.3     */    public boolean contentEquals(File f1, File f2, boolean textfile) throws IOException {        return ResourceUtils.contentEquals(new FileResource(f1), new FileResource(f2), textfile);    }    /**     * 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).     *     * @param f the file whose parent is required.     * @return the given file's parent, or null if the file does not have a parent.     * @since 1.10     * @deprecated since 1.7. Just use {@link File#getParentFile} directly.     */    public File getParentFile(File f) {        return (f == null) ? null : f.getParentFile();    }    /**     * Read from reader till EOF.     * @param rdr the reader from which to read.     * @return the contents read out of the given reader.     *     * @throws IOException if the contents could not be read out from the     *         reader.     */    public static String readFully(Reader rdr) throws IOException {        return readFully(rdr, BUF_SIZE);    }    /**

⌨️ 快捷键说明

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