io.manasobi.utils.FileUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.manasobi.utils.FileUtils.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed 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 io.manasobi.utils;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.*;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * ? ?  , ,  ?  ? ? .<br>
 * 
 * @author manasobi
 * @since 1.0.0
 */
public final class FileUtils {

    private FileUtils() {
    }

    private static Result buildFailResult(Result result, String errMsg) {

        result = Result.FAIL;
        result.setMessage(errMsg);

        return result;
    }

    /**
      * byte? ? ? ? ?? EB, PB, TB, GB, MB, KB, bytes   .
      * 
      * @param file ? ? ? ?
      * @return EB, PB, TB, GB, MB, KB, bytes ? ? ?
      */
    public static String byteCountToDisplaySize(File file) {
        return org.apache.commons.io.FileUtils.byteCountToDisplaySize(file.length());
    }

    /**
      * byte? ? ? ? ?? EB, PB, TB, GB, MB, KB, bytes   .
      * 
      * @param size bytes ? ? ?
      * @return EB, PB, TB, GB, MB, KB, bytes ? ? ?
      */
    public static String byteCountToDisplaySize(long size) {
        return org.apache.commons.io.FileUtils.byteCountToDisplaySize(size);
    }

    /**
     *   ? ? ?   .    .
     * 
     * @param dir   File
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result cleanDir(File dir) {

        Result result = Result.EMPTY;

        if (notExistsDir(dir)) {
            return buildFailResult(result, dir + "  .");
        }

        if (isNotDir(dir)) {
            return buildFailResult(result, dir + "  .");
        }

        try {
            org.apache.commons.io.FileUtils.cleanDirectory(dir);
            return Result.SUCCESS;
        } catch (Exception e) {
            return buildFailResult(result, e.getMessage());
        }
    }

    /**
     *   ? ? ?   .    .
     * 
     * @param dir   
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result cleanDir(String dir) {
        return cleanDir(new File(dir));
    }

    /**
     * ? ?  . ? ? ? ? ? .
     * 
     * @param srcDir  ? 
     * @param destDir ? 
     * @param extList ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyDirAfterCheckFileExt(File srcDir, File destDir, String... extList) {
        return copyDirAfterCheckFileExt(srcDir, destDir, true, extList);
    }

    /**
     * ? ?  . ? ? ? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcDir  ? 
     * @param destDir ? 
     * @param extList ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyDirAfterCheckFileExt(File srcDir, File destDir, boolean preserveFileDate,
            String... extList) {

        Result result = Result.EMPTY;

        if (notExistsDir(srcDir)) {
            return buildFailResult(result, srcDir + "  .");
        }

        if (isNotDir(srcDir)) {
            return buildFailResult(result, srcDir + "  .");
        }

        if (notExistsDir(destDir)) {

            result = createDir(destDir.getAbsolutePath());

            if (result == Result.FAIL) {
                return buildFailResult(result,
                        destDir + "  ? ? ? ?.");
            }
        }

        if (isNotDir(destDir)) {
            return buildFailResult(result, destDir + "  .");
        }

        IOFileFilter suffixFilters = new SuffixFileFilter(extList, IOCase.INSENSITIVE);
        FileFilter filter = FileFilterUtils.or(DirectoryFileFilter.DIRECTORY, suffixFilters);

        try {
            org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir, filter, preserveFileDate);
        } catch (Exception e) {
            return buildFailResult(result, e.getMessage());
        }

        return Result.SUCCESS;
    }

    /**
     * ? ?  . ? ? ? ? ? .
     * 
     * @param srcDir  ? 
     * @param destDir ? 
     * @param extList ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyDirAfterCheckFileExt(String srcDir, String destDir, String... extList) {
        return copyDirAfterCheckFileExt(srcDir, destDir, true, extList);
    }

    /**
     * ? ?  . ? ? ? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcDir  ? 
     * @param destDir ? 
     * @param extList ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyDirAfterCheckFileExt(String srcDir, String destDir, boolean preserveFileDate,
            String... extList) {
        return copyDirAfterCheckFileExt(new File(srcDir), new File(destDir), preserveFileDate, extList);
    }

    /**
     * ? ?? ? ? 
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFile(File srcFile, File destFile) {
        return copyFile(srcFile, destFile, true);
    }

    /**
     * ? ?? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFile(File srcFile, File destFile, boolean preserveFileDate) {

        Result result = Result.EMPTY;

        if (notExistsFile(srcFile)) {
            return buildFailResult(result, srcFile.getName() + "  .");
        }

        if (isNotFile(srcFile)) {
            return buildFailResult(result, srcFile.getName() + "  .");
        }

        if (existsFile(destFile)) {

            result = deleteFile(destFile);

            if (result == Result.FAIL) {
                return buildFailResult(result,
                        destFile.getName() + "?  ?? ? ?.");
            }
        }

        try {
            org.apache.commons.io.FileUtils.copyFile(srcFile, destFile, preserveFileDate);
        } catch (IOException e) {

            String errorMsg = e.getMessage();

            if (errorMsg.contains("same")) {
                return Result.SUCCESS;
            } else {
                return buildFailResult(result, e.getMessage());
            }
        }

        return Result.SUCCESS;
    }

    /**
     * ? ?? ? ? 
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFile(String srcFile, String destFile) {
        return copyFile(new File(srcFile), new File(destFile), true);
    }

    /**
     * ? ?? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFile(String srcFile, String destFile, boolean preserveFileDate) {
        return copyFile(new File(srcFile), new File(destFile), preserveFileDate);
    }

    /**
     * ?? ? ? .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFileToDir(File srcFile, File destDir) {
        return copyFileToDir(srcFile, destDir, true);
    }

    /**
     * ?? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFileToDir(File srcFile, File destDir, boolean preserveFileDate) {

        Result result = Result.EMPTY;

        if (!srcFile.exists()) {
            return buildFailResult(result, srcFile + "  .");
        }

        if (notExistsDir(destDir)) {

            result = createDir(destDir.getAbsolutePath());

            if (result == Result.FAIL) {
                return buildFailResult(result,
                        destDir + "  ? ? ? ?.");
            }
        }

        if (isNotDir(destDir)) {
            return buildFailResult(result, destDir + "  .");
        }

        try {
            org.apache.commons.io.FileUtils.copyFileToDirectory(srcFile, destDir, preserveFileDate);
        } catch (Exception e) {
            return buildFailResult(result, e.getMessage());
        }

        return Result.SUCCESS;
    }

    /**
     * ?? ? ? .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFileToDir(String srcFile, String destDir) {
        return copyFileToDir(new File(srcFile), new File(destDir), true);
    }

    /**
     * ?? ? ? .<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result copyFileToDir(String srcFile, String destDir, boolean preserveFileDate) {
        return copyFileToDir(new File(srcFile), new File(destDir), preserveFileDate);
    }

    /**
     *    ?.
     * 
     * @param dirPath ?  
     * @return enum ? Result   
     */
    public static Result createDir(String dirPath) {

        try {
            org.apache.commons.io.FileUtils.forceMkdir(new File(dirPath));
        } catch (IOException e) {
            return buildFailResult(Result.FAIL, "directory ?? .");
        }

        return Result.SUCCESS;
    }

    /**
     *   ?  ?? .
     * 
     * @param targetDir  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result deleteDir(File targetDir) {

        Result result = Result.EMPTY;

        if (!targetDir.exists()) {
            return buildFailResult(result, targetDir + "  .");
        }

        if (isNotDir(targetDir)) {
            return buildFailResult(result, targetDir + "(?)  .");
        }

        try {
            org.apache.commons.io.FileUtils.deleteDirectory(targetDir);
        } catch (Exception e) {
            return buildFailResult(result, e.getMessage());
        }

        return Result.SUCCESS;
    }

    /**
     *   ?  ?? .
     * 
     * @param targetDir  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result deleteDir(String targetDir) {
        return deleteDir(new File(targetDir));
    }

    /**
     * ?? .
     * 
     * @param targetFile  File
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result deleteFile(File targetFile) {

        Result result = Result.EMPTY;

        if (!targetFile.exists()) {
            return buildFailResult(result, targetFile + "  .");
        }

        if (isNotFile(targetFile)) {
            return buildFailResult(result, targetFile + "? ?? .");
        }

        try {
            org.apache.commons.io.FileUtils.forceDelete(targetFile);
        } catch (Exception e) {
            return buildFailResult(result, e.getMessage());
        }

        return Result.SUCCESS;
    }

    /**
     * ?? .
     * 
     * @param targetFile  File
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result deleteFile(String targetFile) {
        return deleteFile(new File(targetFile));
    }

    /**
     *  ?  ?.
     * 
     * @param dir  ? 
     * @return ?  
     */
    public static boolean existsDir(File dir) {
        return (dir.exists() && isDir(dir)) ? true : false;
    }

    /**
     *  ?  ?.
     * 
     * @param dir  ? 
     * @return ?  
     */
    public static boolean existsDir(String dir) {
        return existsDir(new File(dir));
    }

    /**
     *  ??  ?.
     * 
     * @param file  ? ?
     * @return ?  
     */
    public static boolean existsFile(File file) {
        return (file.exists() && isFile(file)) ? true : false;
    }

    /**
     *  ??  ?.
     * 
     * @param file  ? ?
     * @return ?  
     */
    public static boolean existsFile(String file) {
        return existsFile(new File(file));
    }

    /**
     *  ?  ?.
     * 
     * @param dir  ? 
     * @return ?  
     */
    public static boolean notExistsDir(File dir) {
        return !existsDir(dir);
    }

    /**
     *  ?  ?.
     * 
     * @param dir  ? 
     * @return ?  
     */
    public static boolean notExistsDir(String dir) {
        return !existsDir(new File(dir));
    }

    /**
     *  ??  ?.
     * 
     * @param file  ? ?
     * @return ?  
     */
    public static boolean notExistsFile(File file) {
        return !existsFile(file);
    }

    /**
     *  ??  ?.
     * 
     * @param file  ? ?
     * @return ?  
     */
    public static boolean notExistsFile(String file) {
        return !existsFile(new File(file));
    }

    /**
      * Java ?  ??  .
      * 
      * @return  ?? 
      */
    public static String getCurrentDir() {
        return System.getProperty("user.dir");
    }

    /**
      * Java ?  ??  File ? .
      * 
      * @return     File ?.
      */
    public static File getCurrentDirAsFile() {
        return new File(getUserHomeDir());
    }

    /**
     *  ?  ? .
     * 
     * @param file 
     * @return  ?    
     */
    public static String getParenet(String file) {
        return new File(file).getParent();
    }

    /**
      * system temporary directory  .
      * 
      * @return system temporary directory 
      */
    public static String getTempDir() {
        return System.getProperty("java.io.tmpdir");
    }

    /**
      * system temporary directory  File ? .
      * 
      * @return system temporary directory   File ?. 
      */
    public static File getTempDirAsFile() {
        return new File(getTempDir());
    }

    /**
      * user's home directory  .
      * 
      * @return user's home directory 
      */
    public static String getUserHomeDir() {
        return System.getProperty("user.home");
    }

    /**
      * user's home directory  File ? .
      * 
      * @return user's home directory   File ?.
      */
    public static File getUserHomeDirAsFile() {
        return new File(getUserHomeDir());
    }

    /**
     *  target? ? ?.
     * 
     * @param dir 
     * @return  ?? ? true,  false
     */
    public static boolean isDir(File dir) {
        return (!dir.isDirectory()) ? false : true;
    }

    /**
     *  target? ? ?.
     * 
     * @param dir 
     * @return  ?? ? true,  false
     */
    public static boolean isDir(String dir) {
        return isDir(new File(dir));
    }

    /**
     *  target? ?? ?.
     * 
     * @param file ?
     * @return  ?? ?? true,  false
     */
    public static boolean isFile(File file) {
        return (!file.isFile()) ? false : true;
    }

    /**
     *  target? ?? ?.
     * 
     * @param file ?
     * @return  ?? ?? true,  false
     */
    public static boolean isFile(String file) {
        return isFile(new File(file));
    }

    /**
     *  target?   ?.
     * 
     * @param dir 
     * @return  ??   true,  false
     */
    public static boolean isNotDir(File dir) {
        return (!dir.isDirectory()) ? true : false;
    }

    /**
     *  target?   ?.
     * 
     * @param dir 
     * @return  ??   true,  false
     */
    public static boolean isNotDir(String dir) {
        return isNotDir(new File(dir));
    }

    /**
     *  target? ??  ?.
     * 
     * @param file ?
     * @return  ?? ??  true,  false
     */
    public static boolean isNotFile(File file) {
        return (!file.isFile()) ? true : false;
    }

    /**
     *  target? ??  ?.
     * 
     * @param file ?
     * @return  ?? ??  true,  false
     */
    public static boolean isNotFile(String file) {
        return isNotFile(new File(file));
    }

    /**
     *   ?  ?? . 
     * 
     * @param dir 
     * @param includeRootDir  ?? ? ? . (true ? ?? ?)
     * @return  ?  ??  
     */
    public static List<String> listFileAndDirNames(String dir, boolean includeRootDir) {

        List<File> dirs = (List<File>) org.apache.commons.io.FileUtils.listFilesAndDirs(new File(dir),
                FileFileFilter.FILE, DirectoryFileFilter.DIRECTORY);

        List<String> dirNameList = new ArrayList<String>();

        int index = 0;

        for (File dirUnit : dirs) {

            if (index++ == 0 && !includeRootDir) {
                continue;
            }

            dirNameList.add(dirUnit.getAbsolutePath());
            index++;
        }

        return dirNameList;
    }

    /**
     *   ?  ?? File ?  . 
     * 
     * @param dir 
     * @param includeRootDir  ?? ? ? . (true ? ?? ?)
     * @return  ?  ??  File ? 
     */
    public static File[] listFilesAndDirs(String dir, boolean includeRootDir) {

        List<File> filesAndDirs = (List<File>) org.apache.commons.io.FileUtils.listFilesAndDirs(new File(dir),
                FileFileFilter.FILE, DirectoryFileFilter.DIRECTORY);

        List<File> filesAndDirsList = new ArrayList<File>();

        int index = 0;

        for (File fileOrDir : filesAndDirs) {

            if (index++ == 0 && !includeRootDir) {
                continue;
            }

            filesAndDirsList.add(fileOrDir);
            index++;
        }

        return filesAndDirsList.toArray(new File[filesAndDirsList.size()]);

    }

    /**
     *   ? ? . 
     * 
     * @param dir 
     * @param includeRootDir  ?? ? ? . (true ? ?? ?)
     * @return  ? ?  
     */
    public static List<String> listDirNames(String dir, boolean includeRootDir) {

        List<File> dirs = (List<File>) org.apache.commons.io.FileUtils.listFilesAndDirs(new File(dir),
                new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY);

        List<String> dirNameList = new ArrayList<String>();

        int index = 0;

        for (File dirUnit : dirs) {

            if (index++ == 0 && !includeRootDir) {
                continue;
            }

            dirNameList.add(dirUnit.getAbsolutePath());
            index++;
        }

        return dirNameList;
    }

    /**
     *   ? ? File ?  . 
     * 
     * @param dir 
     * @param includeRootDir  ?? ? ? . (true ? ?? ?)
     * @return  ? ?  File ? 
     */
    public static File[] listDirs(String dir, boolean includeRootDir) {

        List<File> dirs = (List<File>) org.apache.commons.io.FileUtils.listFilesAndDirs(new File(dir),
                new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY);

        List<File> dirList = new ArrayList<File>();

        int index = 0;

        for (File dirUnit : dirs) {

            if (index++ == 0 && !includeRootDir) {
                continue;
            }

            dirList.add(dirUnit);
            index++;
        }

        return dirList.toArray(new File[dirList.size()]);
    }

    /**
     *   ? ?? File ?  . 
     * 
     * @param dir 
     * @param recursive  ?? ? ??  ? 
     * @return  ? ??  File ? 
     */
    public static File[] listFiles(String dir, boolean recursive) {

        return org.apache.commons.io.FileUtils.convertFileCollectionToFileArray(
                org.apache.commons.io.FileUtils.listFiles(new File(dir), null, recursive));
    }

    /**
     *   ? ??   .
     * 
     * @param dir 
     * @param recursive  ?? ? ??  ? 
     * @return  ? ??  ? 
     */
    public static List<String> listFileNames(String dir, boolean recursive) throws FileUtilsException {

        Collection<File> files = null;

        try {
            files = org.apache.commons.io.FileUtils.listFiles(new File(dir), null, recursive);
        } catch (Exception e) {
            throw new FileUtilsException(e.getMessage());
        }

        List<String> fileNamesList = new ArrayList<String>();

        for (File file : files) {
            fileNamesList.add(file.getAbsolutePath());
        }

        return fileNamesList;
    }

    /**
     *  ? ? ? ??   .
     * 
     * @param dir  
     * @param recursive  ?? ? ??   
     * @param extList ? ? 
     * @return  ? ? ? ??  
     */
    public static List<String> listFilenamesIncludeExt(String dir, boolean recursive, String... extList) {

        List<File> extFilterList = (List<File>) org.apache.commons.io.FileUtils.listFiles(new File(dir), extList,
                recursive);

        List<String> resultList = new ArrayList<String>();

        for (File file : extFilterList) {
            resultList.add(file.getAbsolutePath());
        }

        return resultList;
    }

    /**
     *  ? ? ? ?? File ?  .
     * 
     * @param dir  
     * @param recursive  ?? ? ??   
     * @param extList ? ? 
     * @return  ? ? ? ??  File ? 
     */
    public static File[] listFilesIncludeExt(String dir, boolean recursive, String... extList) {

        Collection<File> resultFiles = org.apache.commons.io.FileUtils.listFiles(new File(dir), extList, recursive);

        return org.apache.commons.io.FileUtils.convertFileCollectionToFileArray(resultFiles);
    }

    /**
     *  ? ?  ?? ?  .
     * 
     * @param dir  
     * @param recursive  ?? ? ??   
     * @param extList ? ? 
     * @return  ? ?  ?? ? 
     */
    public static List<String> listFilenamesExcludeExt(String dir, boolean recursive, String... extList) {

        IOFileFilter suffixFileFilters = new SuffixFileFilter(extList, IOCase.INSENSITIVE);
        IOFileFilter excludeExtFilter = FileFilterUtils.notFileFilter(FileFilterUtils.or(suffixFileFilters));

        List<File> resultFiles = (List<File>) org.apache.commons.io.FileUtils.listFiles(new File(dir),
                excludeExtFilter, TrueFileFilter.INSTANCE);

        List<String> resultList = new ArrayList<String>();

        for (File file : resultFiles) {
            resultList.add(file.getAbsolutePath());
        }

        return resultList;
    }

    /**
     *  ? ?  ?? File ?  .
     * 
     * @param dir  
     * @param recursive  ?? ? ??   
     * @param extList ? ? 
     * @return  ? ?  ??  File ? 
     */
    public static File[] listFilesExcludeExt(String dir, boolean recursive, String... extList) {

        IOFileFilter suffixFileFilters = new SuffixFileFilter(extList, IOCase.INSENSITIVE);
        IOFileFilter excludeExtFilter = FileFilterUtils.notFileFilter(FileFilterUtils.or(suffixFileFilters));

        Collection<File> resultFiles = org.apache.commons.io.FileUtils.listFiles(new File(dir), excludeExtFilter,
                TrueFileFilter.INSTANCE);

        return org.apache.commons.io.FileUtils.convertFileCollectionToFileArray(resultFiles);
    }

    /**
     * ? ?? ? ?  ??.
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFile(File srcFile, File destFile) {
        return moveFile(srcFile, destFile, true);
    }

    /**
     * ? ?? ? ?  ??.
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFile(String srcFile, String destFile) {
        return moveFile(new File(srcFile), new File(destFile), true);
    }

    /**
     * ? ?? ? ?  ??.<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFile(File srcFile, File destFile, boolean preserveFileDate) {

        Result result = Result.EMPTY;

        if (!srcFile.exists()) {
            return buildFailResult(result, srcFile + "  .");
        }

        if (isNotFile(srcFile)) {
            return buildFailResult(result, srcFile + " ?? .");
        }

        if (destFile.exists()) {

            result = deleteFile(destFile);

            if (result == Result.FAIL) {
                return buildFailResult(result, destFile + " ? ? ? ?.");
            }
        }

        result = copyFile(srcFile, destFile, preserveFileDate);

        if (result == Result.FAIL) {
            return buildFailResult(result, srcFile + " ? ? ? ?.");
        }

        result = deleteFile(srcFile);

        if (result == Result.FAIL) {
            return buildFailResult(result, srcFile + " ? ? ? ?.");
        }

        return Result.SUCCESS;
    }

    /**
     * ? ?? ? ?  ??.<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile  ? ?
     * @param destFile ? ?
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFile(String srcFile, String destFile, boolean preserveFileDate) {
        return moveFile(new File(srcFile), new File(destFile), preserveFileDate);
    }

    /**
     * ? ?? ?   ??.
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFileToDir(File srcFile, File destDir) {
        return moveFileToDir(srcFile, destDir, true);
    }

    /**
     * ? ?? ?   ??.
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFileToDir(String srcFile, String destDir) {
        return moveFileToDir(new File(srcFile), new File(destDir), true);
    }

    /**
     * ? ?? ?   ??.<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @param preserveFileDate ?  
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFileToDir(File srcFile, File destDir, boolean preserveFileDate) {

        Result result = Result.EMPTY;

        if (isNotFile(srcFile)) {
            return buildFailResult(result, srcFile + "? ?? .");
        }

        if (FileUtils.notExistsDir(destDir)) {

            result = createDir(destDir.getAbsolutePath());

            if (result == Result.FAIL) {
                return buildFailResult(result,
                        destDir + "  ? ? ? ?.");
            }
        }

        if (isNotDir(destDir)) {
            return buildFailResult(result, srcFile + "  .");
        }

        String destFile = destDir + File.separator + srcFile.getName();

        result = moveFile(srcFile, new File(destFile), preserveFileDate);

        if (result == Result.FAIL) {
            return buildFailResult(result, srcFile + " ?? ? ? ?.");
        }

        return Result.SUCCESS;
    }

    /**
     * ? ?? ?   ??.<br>
     * preserveFileDate true ?? ?  ??   false  ??  ?
     * .
     * 
     * @param srcFile ? ?
     * @param destDir ? 
     * @param preserveFileDate ?              
     * @return  enum ? Result.SUCCESS   Result.FAIL? 
     */
    public static Result moveFileToDir(String srcFile, String destDir, boolean preserveFileDate) {
        return moveFileToDir(new File(srcFile), new File(destDir), preserveFileDate);
    }

    /**
     * ? ?? FileInputStream .<br>
     * 
     * @param file ? ?
     * @return ? ??  FileInputStream
     */
    public static FileInputStream openInputStream(File file) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }

            if (!file.canRead()) {
                return null;
            }

        } else {
            return null;
        }

        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return fis;
    }

    /**
     * ? ?? FileInputStream .<br>
     * 
     * @param filePath ? ? 
     * @return ? ??  FileInputStream
     */
    public static FileInputStream openInputStream(String filePath) {
        return openInputStream(new File(filePath));
    }

    /**
     * ? ?? outputStream .
     * 
     * @param file ? ?
     * @return ? ??  FileOutputStream
     */
    public static FileOutputStream openOutputStream(File file) {
        return openOutputStream(file, false);
    }

    /**
     * ? ?? outputStream . append true?   ??  ? ??.
     * 
     * @param file   ? ?
     * @param append  ?? ? ???  
     * @return ? ??  FileOutputStream
     */
    public static FileOutputStream openOutputStream(File file, boolean append) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }
            if (!file.canWrite()) {
                return null;
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    return null;
                }
            }
        }

        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file, append);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fos;
    }

    /**
     * ? ?? outputStream .
     * 
     * @param filePath ? ? 
     * @return ? ??  FileOutputStream
     */
    public static FileOutputStream openOutputStream(String filePath) {
        return openOutputStream(new File(filePath), false);
    }

    /**
     * ? ?? outputStream . append true?   ??  ? ??.
     * 
     * @param filePath ? ? 
     * @param append    ?? ? ???  
     * @return ? ??  FileOutputStream
     */
    public static FileOutputStream openOutputStream(String filePath, boolean append) {
        return openOutputStream(new File(filePath), append);
    }

    /**
     * ? ?? FileReader .
     * 
     * @param file ? ?
     * @return ? ??  FileReader
     */
    public static FileReader openReader(File file) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }

            if (!file.canRead()) {
                return null;
            }

        } else {
            return null;
        }

        FileReader fileReader = null;

        try {
            fileReader = new FileReader(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileReader;
    }

    /**
     * ? ?? FileReader .
     * 
     * @param filePath ? ? 
     * @return ? ??  FieReader
     */
    public static FileReader openReader(String filePath) {
        return openReader(new File(filePath));
    }

    /**
    * ? ?? FileWriter .
    * 
    * @param file ? ?
    * @return ? ??  FileWriter
    */
    public static FileWriter openWriter(File file) {
        return openWriter(file, false);
    }

    /**
    * ? ?? FileWriter . append true?   ??  ? ??.
    * 
    * @param file ? ?
    * @param append  ?? ? ???  
    * @return ? ??  FileWriter
    */
    public static FileWriter openWriter(File file, boolean append) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }
            if (!file.canWrite()) {
                return null;
            }
        } else {

            File parent = file.getParentFile();

            if (parent != null) {

                if (!parent.mkdirs() && !parent.isDirectory()) {
                    return null;
                }
            }
        }

        FileWriter fileWriter = null;

        try {
            fileWriter = new FileWriter(file, append);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileWriter;
    }

    /**
    * ? ?? FileWriter .
    * 
    * @param filePath ? ? 
    * @return ? ??  FileWriter
    */
    public static FileWriter openWriter(String filePath) {
        return openWriter(new File(filePath));
    }

    /**
    * ? ?? FileWriter . append true?   ??  ? ??.
    * 
    * @param filePath ? ? 
    * @param append  ?? ? ???  
    * @return ? ??  FileWriter
    */
    public static FileWriter openWriter(String filePath, boolean append) {
        return openWriter(new File(filePath), append);
    }

    /**
     * ? ?? BufferedWriter .
     * 
     * @param file ? ?
     * @return ? ??  FileWriter
     */
    public static BufferedWriter openBufferWriter(File file) {
        return openBufferWriter(file, "UTF-8", false);
    }

    /**
     * ? ?? BufferedWriter .
     * 
     * @param file ? ?
     * @param charSet ??  ?
     * @return ? ??  FileWriter
     */
    public static BufferedWriter openBufferWriter(File file, String charSet) {
        return openBufferWriter(file, charSet, false);
    }

    /**
    * ? ?? BufferedWriter . append true?   ??  ? ??.
    * 
    * @param file ? ?
    * @param charSet ??  ?
    * @param append  ?? ? ???  
    * @return ? ??  FileWriter
    */
    public static BufferedWriter openBufferWriter(File file, String charSet, boolean append) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }

            if (!file.canWrite()) {
                return null;
            }

        } else {

            File parent = file.getParentFile();

            if (parent != null) {

                if (!parent.mkdirs() && !parent.isDirectory()) {
                    return null;
                }
            }
        }

        BufferedWriter bufferedWriter = null;

        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charSet));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bufferedWriter;
    }

    /**
    * ? ?? BufferedWriter .
    * 
    * @param filePath ? ? 
    * @return ? ??  FileWriter
    */
    public static BufferedWriter openBufferWriter(String filePath) {
        return openBufferWriter(new File(filePath), "UTF-8", false);
    }

    /**
     * ? ?? BufferedWriter .
     * 
     * @param filePath ? ? 
     * @param charSet ??  ?
     * @return ? ??  FileWriter
     */
    public static BufferedWriter openBufferWriter(String filePath, String charSet) {
        return openBufferWriter(new File(filePath), charSet, false);
    }

    /**
    * ? ?? BufferedWriter . append true?   ??  ? ??.
    * 
    * @param filePath ? ? 
    * @param append  ?? ? ???  
    * @return ? ??  FileWriter
    */
    public static BufferedWriter openBufferWriter(String filePath, boolean append) {
        return openBufferWriter(new File(filePath), "UTF-8", append);
    }

    /**
     * ? ?? BufferedWriter . append true?   ??  ? ??.
     * 
     * @param filePath ? ? 
     * @param charSet ??  ?
     * @param append  ?? ? ???  
     * @return ? ??  FileWriter
     */
    public static BufferedWriter openBufferWriter(String filePath, String charSet, boolean append) {
        return openBufferWriter(new File(filePath), charSet, append);
    }

    /**
    * ?? ? ?  ?  .<br>
    * 
    * @param file ? ?
    * @return ? ?? byte[]
    */
    public static byte[] readFileToByteArray(File file) {

        InputStream in = null;

        try {
            in = openInputStream(file);
            return IOUtils.toByteArray(in, file.length());
        } catch (Exception e) {
            return null;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    /**
    * ?? ? ?  ?  .<br>
    * 
    * @param file ? ? 
    * @return ? ?? byte[]
    */
    public static byte[] readFileToByteArray(String file) {
        return readFileToByteArray(new File(file));
    }

    /**
     * ?? ? ?   charset ? ?? .
     * 
     * @param file ? ? 
     * @param encoding ? charset
     * @return ? ?  charset ? ?
     */
    public static String readFileToString(File file, Charset encoding) {

        InputStream in = null;
        in = openInputStream(file);

        try {
            return IOUtils.toString(in, Charsets.toCharset(encoding));
        } catch (Exception e) {
            return null;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    /**
     * ?? ? ?   charset ? ?? .
     * 
     * @param file ? ? 
     * @param encoding ? charset ?
     * @return ? ?  charset ? ?
     */
    public static String readFileToString(File file, String encoding) {
        return readFileToString(file, Charsets.toCharset(encoding));
    }

    /**
     * ?? ? ?  ?? .
     * 
     * @param file ? ? 
     * @return ? ?  charset ? ?
     */
    public static String readFileToString(File file) {
        return readFileToString(file, Charset.defaultCharset());
    }

    /**
    * ? ?? ? ?  .<br>
    * ? ? ? 
    * 
    * @param srcFile  ? ?
    * @param destFile ? ?
    * @return  enum ? Result.SUCCESS   Result.FAIL? 
    */
    public static Result rename(String srcFile, String destFile) {

        Result result = Result.EMPTY;

        if (!existsFile(srcFile)) {
            return buildFailResult(result, srcFile + "  .");
        }

        if (existsFile(destFile)) {

            result = deleteFile(destFile);

            if (result == Result.FAIL) {
                return buildFailResult(result, destFile + "  ? ? ?");
            }
        }

        File file = new File(srcFile);

        if (!file.renameTo(new File(destFile))) {

            result = copyFile(srcFile, destFile, false);

            if (result == Result.SUCCESS) {

                result = deleteFile(srcFile);

                if (result == Result.FAIL) {
                    deleteFile(destFile);
                }
            }

            return result;

        } else {

            return Result.SUCCESS;
        }
    }

    /*public static void main(String[] args) {
           
       *//*List<File> resultFiles = (List<File>) org.apache.commons.io.FileUtils.listFiles(new File("c:/ePapyrus"), new String[] {"end", "done"},  true);
              
          for (File file : resultFiles) {
           System.out.println(file.getAbsolutePath());
          }*//*
                  
              *//*File[] files = listFilesByExt("c:/epapyrus", true, "end", "done");
                     
                 for (File file : files) {
                  System.out.println(file.getAbsolutePath());
                 }*//*
                         
                     *//*List<String> resultFiles = listFilenamesByWildcard("c:/temp/Scratch", "@MG@*.*", false);
                            
                        for (String file : resultFiles) {
                         System.out.println(file);
                        }
                            
                        File[] files = listFilesByWildcard("c:/temp/Scratch", "@MG@*.*", false);
                            
                        for (File file : files) {
                         System.out.println(file.getAbsolutePath());
                        }
                            
                        List<String> resultFiles2 = listFilenamesByWildcard("c:/temp", "@MG@*.*", true);
                            
                        for (String file : resultFiles2) {
                         System.out.println(file);
                        }
                            
                        File[] files2 = listFilesByWildcard("c:/temp", "@MG@*.*", true);
                            
                        for (File file : files2) {
                         System.out.println(file.getAbsolutePath());
                        }*//*
                                
                            *//*System.out.println(getTempDir());
                               System.out.println(getUserHomeDir());
                               System.out.println(getCurrentDir());*//*
                                                                          
                                                                      *//*IOFileFilter excludeExtFilter = FileFilterUtils.notFileFilter(FileFilterUtils.or(
                                                                             FileFilterUtils.suffixFileFilter(".html"), 
                                                                             FileFilterUtils.suffixFileFilter(".pdf"), 
                                                                             FileFilterUtils.suffixFileFilter(".mht")));
                                                                             
                                                                         IOFileFilter fileFilter = FileFilterUtils.makeFileOnly(excludeExtFilter);*//*
                                                                                                                                                         
                                                                                                                                                     *//*IOFileFilter suffixFileFilters = new SuffixFileFilter(new String[] {"html", "pdf", "mht"}, IOCase.SENSITIVE);
                                                                                                                                                        IOFileFilter excludeExtFilter = FileFilterUtils.notFileFilter(FileFilterUtils.or(suffixFileFilters));
                                                                                                                                                            
                                                                                                                                                        List<File> resultFiles = (List<File>) org.apache.commons.io.FileUtils.listFiles(new File("C:/ePapyrus/Hotfolder/03.InProc/512ee3cc-428e-4341-a446-62e755dc0efe"), excludeExtFilter, TrueFileFilter.INSTANCE);
                                                                                                                                                            
                                                                                                                                                        for (File file : resultFiles) {
                                                                                                                                                         System.out.println(file.getAbsolutePath());
                                                                                                                                                        }*//*
                                                                                                                                                                
                                                                                                                                                                
                                                                                                                                                            *//*List<String> list = FileUtils.listFileAndDirNames("C:/ePapyrus/log", false);
                                                                                                                                                                   
                                                                                                                                                               for (String file : list) {
                                                                                                                                                                System.out.println(file);
                                                                                                                                                               }*//*
                                                                                                                                                                       
                                                                                                                                                                   *//*File[] files = FileUtils.listFilesAndDirs("C:/ePapyrus/log", false);
                                                                                                                                                                          
                                                                                                                                                                      for (File file : files) {
                                                                                                                                                                       System.out.println(file.getAbsolutePath());
                                                                                                                                                                      }*//*
                                                                                                                                                                              
                                                                                                                                                                          //System.out.println(getNumPagesOfMultiTif("c:/ePapyrus/HotFolder/3.tif"));
                                                                                                                                                                              
                                                                                                                                                                          List<String> list = listExcludeFilenamesByWildcard("c:/test", new String[] {"seq-*.*"}, false); 
                                                                                                                                                                              
                                                                                                                                                                          for (String str : list) {
                                                                                                                                                                          System.out.println(str);         
                                                                                                                                                                          }
                                                                                                                                                                              
                                                                                                                                                                          File[] files = listExcludeFilesByWildcard("c:/test", new String[] {"seq-*.*"}, false); 
                                                                                                                                                                              
                                                                                                                                                                          for (File file : files) {
                                                                                                                                                                          System.out.println(file.getAbsolutePath());         
                                                                                                                                                                          System.out.println(file.getName());         
                                                                                                                                                                          }
                                                                                                                                                                              
                                                                                                                                                                          }*/

}