📄 fileutils.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.util;import java.io.File;import java.io.FilenameFilter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.Reader;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.net.MalformedURLException;import java.net.URL;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.Random;import java.util.Stack;import java.util.StringTokenizer;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.PathTokenizer;import org.apache.tools.ant.Project;import org.apache.tools.ant.launch.Locator;import org.apache.tools.ant.taskdefs.condition.Os;import org.apache.tools.ant.types.FilterSetCollection;import 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. * */public class FileUtils { private static final int EXPAND_SPACE = 50; private static final FileUtils PRIMARY_INSTANCE = new FileUtils(); //get some non-crypto-grade randomness from various places. private static Random rand = new Random(System.currentTimeMillis() + Runtime.getRuntime().freeMemory()); private static final boolean ON_NETWARE = Os.isFamily("netware"); private static final boolean ON_DOS = Os.isFamily("dos"); private static final boolean ON_WIN9X = Os.isFamily("win9x"); private static final boolean ON_WINDOWS = Os.isFamily("windows"); static final int BUF_SIZE = 8192; /** * The granularity of timestamps under FAT. */ public static final long FAT_FILE_TIMESTAMP_GRANULARITY = 2000; /** * The granularity of timestamps under Unix. */ public static final long UNIX_FILE_TIMESTAMP_GRANULARITY = 1000; /** * 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. */ public static final long NTFS_FILE_TIMESTAMP_GRANULARITY = 1; /** * A one item cache for fromUri. * fromUri is called for each element when parseing ant build * files. It is a costly operation. This just caches the result * of the last call. */ private Object cacheFromUriLock = new Object(); private String cacheFromUriRequest = null; private String cacheFromUriResponse = null; /** * Factory method. * * @return a new instance of FileUtils. * @deprecated since 1.7. * Use getFileUtils instead, * FileUtils do not have state. */ public static FileUtils newFileUtils() { return new FileUtils(); } /** * Method to retrieve The FileUtils, which is shared by all users of this * method. * @return an instance of FileUtils. * @since Ant 1.6.3 */ public static FileUtils getFileUtils() { return PRIMARY_INSTANCE; } /** * Empty constructor. */ protected FileUtils() { } /** * Get the URL for a file taking into account # characters. * * @param file the file whose URL representation is required. * @return The FileURL value. * @throws MalformedURLException if the URL representation cannot be * formed. */ public URL getFileURL(File file) throws MalformedURLException { return new URL(toURI(file.getAbsolutePath())); } /** * Convenience method to copy a file from a source to a destination. * No filtering is performed. * * @param sourceFile Name of file to copy from. * Must not be <code>null</code>. * @param destFile Name of file to copy to. * Must not be <code>null</code>. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile) throws IOException { copyFile(new File(sourceFile), new File(destFile), null, false, false); } /** * Convenience method to copy a file from a source to a destination * specifying if token filtering must be used. * * @param sourceFile Name of file to copy from. * Must not be <code>null</code>. * @param destFile Name of file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, false, false); } /** * 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. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of 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. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, false); } /** * 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 <code>destFile</code> file should be made equal to * the last modified time * of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of 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(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, 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 <code>destFile</code> file should be made equal to the last modified time * of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of 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(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified, String encoding) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, preserveLastModified, encoding); } // CheckStyle:ParameterNumberCheck OFF - bc /** * 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 Name of file to copy from. * Must not be <code>null</code>. * @param destFile Name of 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(String sourceFile, String destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String encoding, Project project) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, filterChains, overwrite, preserveLastModified, 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 Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of 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(String sourceFile, String destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project project) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, filterChains, overwrite, preserveLastModified, inputEncoding, outputEncoding, project); } /** * Convenience method to copy a file from a source to a destination. No filtering is performed. * * @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>. * * @throws IOException if the copying fails.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -