日期:2014-05-17  浏览次数:20852 次

org.apache.common.io---FileUtils详解
getTempDirectoryPath():返回临时目录路径;
 public static String getTempDirectoryPath() {
        return System.getProperty("java.io.tmpdir");
    }

getTempDirectory():返回临时目录文件路径的File实例;
public static File getTempDirectory() {
        return new File(getTempDirectoryPath());
    }

getUserDirectoryPath():返回用户目录路径;
public static String getUserDirectoryPath() {
        return System.getProperty("user.home");
    }

getUserDirectory():返回用户目录路径的File实例
public static File getUserDirectory() {
        return new File(getUserDirectoryPath());
    }

openInputStream(File file):通过指定文件创建一个输入流;如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
public static FileOutputStream openOutputStream(File file) throws IOException {
        return openOutputStream(file, false);
    }

public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file, append);
    }

byteCountToDisplaySize(long size):返回指定数值的计算机表示(KB,MB,GB);
/**
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;

    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;
/**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;
public static String byteCountToDisplaySize(long size) {
        String displaySize;

//        if (size / ONE_EB > 0) {
//            displaySize = String.valueOf(size / ONE_EB) + " EB";
//        } else if (size / ONE_PB > 0) {
//            displaySize = String.valueOf(size / ONE_PB) + " PB";
//        } else if (size / ONE_TB > 0) {
//            displaySize = String.valueOf(size / ONE_TB) + " TB";
//        } else 
        if (size / ONE_GB > 0) {
            displaySize = String.valueOf(size / ONE_GB) + " GB";
        } else if (size / ONE_MB > 0) {
            displaySize = String.valueOf(size / ONE_MB) + " MB";
        } else if (size / ONE_KB > 0) {
            displaySize = String.valueOf(size / ONE_KB) + " KB";
        } else {
            displaySize = String.valueOf(size) + " bytes";
        }
        return displaySize;
    }

touch(File file):更新指定文件最终修改时间和访问时间;若文件不存在则生成空文件;Closeable 是可以关闭的数据源或目标。调用 close 方法可释放对象保存的资源(如打开文件)。
public static void touch(File file) throws IOException {
        if (!file.exists()) {
            OutputStream out = openOutputStream(file);
            IOUtils.closeQuietly(out);
        }
        boolean success = file.setLastModified(System.currentTimeMillis());
        if (!success) {
            throw new IOException("Unable to set the last modification time for " + file);
        }
    }
public static void closeQuietly(OutputStream output) {
        closeQuietly((Closeable)output);
    }
public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }