天天看点

拷贝文件和文件夹

package ums.zxcomc.pm.maf.cbwa.pmpcommon.comm.pmcollect;

import java.io.*;

import ums.uep.api.util.DebugPrn;

import ums.zxcomc.pm.maf.cbwa.pmpcommon.comm.PmpMafConst;

public class CbwaFileCopyManager {

    private static DebugPrn debugPrn = new DebugPrn(CbwaPmCollectInfoExport.class.getName());

    public boolean copyFileOrFolder(String srcFileName, String destFileName, boolean overlay){

        File srcFile = new File(srcFileName);

        // 判断源文件(夹)是否存在

        if (!srcFile.exists()) {

            debugPrn.info("The source file/folder doesn't exist: " +  srcFileName);

            return false; 

        }

        if(srcFile.isFile())

        {

            //拷贝文件

            boolean isOK = copyFile(srcFileName, destFileName, overlay);

            if(!isOK)

            {

                debugPrn.warn("Fail to copy file: " +  srcFileName);

                return false; 

            }

        }

        else if(srcFile.isDirectory())

        {

            //拷贝文件夹

            boolean isOK = copyFolder(srcFileName, destFileName, overlay);

            if(!isOK)

            {

                debugPrn.warn("Fail to copy folder: " +  srcFileName);

                return false; 

            }

        }

        else

        {

            debugPrn.info("The source file/folder isn't valid : " +  srcFileName);

            return false;

        }

        return true;

    }

    private boolean copyFile(String srcFileName, String destFileName, boolean overlay){

        // 判断目标文件是否存在

        File destFile = new File(destFileName);

        if (destFile.exists()) {

            //如果目标文件存在

            if (overlay) {

                // 允许覆盖文件:则先删除目标文件

                if(!destFile.delete())

                {

                    //如果删除失败

                    debugPrn.warn("Fail to delete the File:" + destFileName);

                    return false;

                }

            }

            else

            {

                //不允许覆盖,直接返回,不视为拷贝失败

                debugPrn.info("Not allowed to overwrite the file: " + destFileName);

                return true;

            }

        } else {

            // 如果目标文件所在目录不存在,则先创建目录

            if (!destFile.getParentFile().exists()) {

                // 目标文件所在目录不存在

                if (!destFile.getParentFile().mkdirs()) {

                    // 创建目标文件所在目录失败

                    debugPrn.warn("Fail to mkdirs by the path: " +  destFileName);

                    return false;

                }

            }

        }

        //开始复制文件

        File srcFile = new File(srcFileName);

        int byteread = 0; //读取的字节数

        InputStream in = null;

        OutputStream out = null;

        BufferedOutputStream bufferedOut = null;

        BufferedInputStream bufferedIn = null;

        try {

            in = new FileInputStream(srcFile);

            out = new FileOutputStream(destFile);

            bufferedIn = new BufferedInputStream(in);

            bufferedOut= new BufferedOutputStream(out);

            byte[] buffer = new byte[1024];

            while ((byteread = bufferedIn.read(buffer)) != -1) {

                bufferedOut.write(buffer, 0, byteread);

            }

            bufferedOut.flush();

            return true;

        } catch (FileNotFoundException e) {

            debugPrn.warn("FileNotFoundException in copyFile: ", e);

            return false;

        } catch (IOException e) {

            debugPrn.warn("IOException in copyFile: ", e);

            return false;

        } finally{

            if (bufferedOut != null)

            {

                try

                {

                    bufferedOut.close();  

                }

                catch(IOException e)

                {

                    bufferedOut = null;

                    debugPrn.warn("IOException in copyFile: ", e);

                }

            }

            if (bufferedIn != null)

            {

                try

                {

                    bufferedIn.close();  

                }

                catch(IOException e)

                {

                    bufferedIn = null;

                    debugPrn.warn("IOException in copyFile: ", e);

                }

            }

        }

    }

    private boolean copyFolder(String srcFolderName, String destFolderName, boolean overlay){

        File srcFolder = new File(srcFolderName);

        File destFolder = new File(destFolderName);

        if(!destFolder.exists())

        {

            if(!destFolder.mkdirs())

            {

                debugPrn.warn("Fail to mkdirs: " +  destFolderName);

                return false;

            }

        }

        String[] subFileFolderList =  srcFolder.list();

        for(int i = 0; i < subFileFolderList.length; i++)

        {

            String srcSubFileName = srcFolderName + PmpMafConst.SEPARATOR +subFileFolderList[i]; //子文件(夹)的源路径

            String deskSubFileName = destFolderName + PmpMafConst.SEPARATOR + subFileFolderList[i];

            boolean isOK = copyFileOrFolder(srcSubFileName, deskSubFileName, overlay);

            if(!isOK)

            {

                debugPrn.warn("Fail to copy: " +  srcSubFileName);

                return false;

            }

        }

        return true;

    }

}

继续阅读