fileutils.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 230 行

SVN-BASE
230
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.utils;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.nio.channels.FileChannel;/** * Utilities for operating with files and directories. */public final class FileUtils {        /**     * Creation of instances is not allowed.     */    private FileUtils() {    }        /**     * Copies source file to destination file. If destination file     * doesn't exists, it's created.     *     * @param   srcFile     source file.     * @param   destFile    destination file.     * @param   overwrite   whether the destination file should be overwritten     *     * @return  <code>true</code> if the file was successfully copied to     *          destination file, or <code>false</code> if destination file     *          already exists and <code>overwrite</code> is <code>false</code>.     *     * @exception   IOException if I/O error occurs, or destination file cannot     *      be created.     */    public static boolean copyFile(File srcFile, File destFile,            boolean overwrite) throws IOException {        return copyStreamToFile(new FileInputStream(srcFile), destFile,                overwrite);    }        /**     * Copies source file to destination directory. New file is created in the     * destination directory with the same name as the source file. If the      * destination file already exists, it can be overwritten depending on     * value of <code>overwrite</code> parameter.     *     * @param   srcFile     source file.     * @param   destDir     destination directory.     * @param   overwrite   whether the destination file should be overwritten     *     * @return  <code>true</code> if the file was successfully copied to     *          destination directory, or <code>false</code> if file with same     *          name already exists in the destination directory and      *          <code>overwrite</code> is <code>false</code>.     *     * @exception   IOException if I/O error occurs, or destination file cannot     *      be created.     */    public static boolean copyFileToDir(File srcFile, File destDir,            boolean overwrite) throws IOException {        return copyFile(srcFile, new File(destDir, srcFile.getName()),                overwrite);    }        /**     * Copies source file to destination directory. New file is created in the     * destination directory with the same name as the source file. If the      * destination file already exists, it can be overwritten depending on     * value of <code>overwrite</code> parameter. If the destination directory     * doesn't exist, it can be created depending on value of <code>createDir     * </code> parameter.     *     * @param   srcFile     source file.     * @param   destDir     destination directory.     * @param   overwrite   whether the destination file should be overwritten     * @param   createDir   whether the destination directory should be created     *     * @return  <code>true</code> if the file was successfully copied to     *          destination directory, or <code>false</code> if file with same     *          name already exists in the destination directory and      *          <code>overwrite</code> is <code>false</code>. Returns <code>     *          false</code> if destination directory doesn't exist and cannot     *          be created and if <code>createdDir</code> is <code>true</code>.     *     * @exception   IOException if I/O error occurs, or destination file cannot     *      be created.     */    public static boolean copyFileToDir(File srcFile, File destDir,            boolean overwrite, boolean createDir) throws IOException {        if (!destDir.exists()) {            if (!createDir) {                return false;            }            if (!destDir.mkdirs()) {                return false;            }        }        return copyFileToDir(srcFile, destDir, overwrite);    }        /**     * Reads the input stream and copies all read bytes to destination file.      * Stops when the end of the input stream is reached. If destination file     * doesn't exists, it's created.     *     * @param   in  <code>InputStream</code> to copy bytes from.     * @param   destFile    destination file.     * @param   overwrite   whether the destination file should be overwritten     *     * @return  <code>true</code> if the input was successfully copied to     *          destination file, or <code>false</code> if destination file     *          already exists and <code>overwrite</code> is <code>false</code>.     *     * @exception   IOException if I/O error occurs, or destination file cannot     *      be created.     */    public static boolean copyStreamToFile(InputStream in, File destFile,            boolean overwrite) throws IOException {        int bytesRead;        byte [] buffer = new byte[4096];        FileOutputStream out = null;                try {            if (destFile.exists()) {                if (!overwrite) {                    return false;                }            } else {                if (!destFile.createNewFile()) {                    throw new IOException("Cannot create destination file: "                            + destFile.getPath());                }               }            out = new FileOutputStream(destFile, false);            while ((bytesRead = in.read(buffer)) != -1) {                out.write(buffer, 0, bytesRead);            }        } finally {            try {                if (out != null) {                    out.close();                }            } catch (IOException e) {}            try {                in.close();            } catch (IOException e) {}        }                return true;    }    /**     * Clears the content of the specified directory including subdirectories.     * Leaves the specified directory empty.     *     * @param   dir     directory to be cleaned.        *     * @exception   IOException if failed to delete a file or subdirectory.     */    public static void clearDir(File dir) throws IOException {        if (!dir.exists()) {            return;        }        File[] files = dir.listFiles();        for (File f : files) {            if (f.isDirectory()) {                clearDir(f);            }            if (!f.delete()) {                throw new IOException("Cannot delete: " + f.getPath());            }        }    }        /**     * Copies the whole content of the source directory to the destination     * directory. If destination directory doesn't exist, it's created.     *     * @param   srcDir  Source directory.     * @param   destDir Destination directory.     *     * @exception   IOException if an I/O error occurs, or destination directory     *              cannot be created, or some content cannot be copied.     */    public static void copyDir(File srcDir, File destDir) throws IOException {        if (!destDir.exists() && !destDir.mkdirs()) {            throw new IOException("Cannot create directory: "                     + destDir.getPath());        }        File[] content = srcDir.listFiles();        for (File file : content) {            if (file.isDirectory()) {                copyDir(file, new File(destDir, file.getName()));            }            if (file.isFile()) {                copyFileToDir(file, destDir, true);            }        }    }    }

⌨️ 快捷键说明

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