复制文件和目录

public class CopyFile
{
    // 复制单个文件,如果目标存在则不覆盖
    // srcFileName 源文件名
    // destFileName 目标文件名
    public static boolean copyFile(String srcFileName, String destFileName)
    {
        return copyFile(srcFileName, destFileName, false);
    }
 
    // 复制单个文件,如果目标存在则覆盖
    public static boolean copyFile(String srcFileName, String destFileName, boolean overlay)
    {
        File file = new File(srcFileName);
        if (!file.exists())
        {
            System.out.println("复制失败源文件" + srcFileName + "不存在");
            return false;
        } else if (!file.isFile())
        {
            System.out.println("复制失败" + srcFileName + "不是一个文件");
            return false;
        }
 
        File file2 = new File(destFileName);
        if (file2.exists())
        {
            if (overlay)
            {
                System.out.println("目标文件已经存在准备删除它");
                if (DeletFile.delete(destFileName) == false)
                {
 
                    System.out.println("文件复制失败,删除已经存在的目标文件失败");
                    return false;
                }
            } else
            {
                System.out.println("复制文件失败,目标文件已经存在!");
                return false;
            }
        } else
        // 如果目标文件所在的目录不存在则创建它
        {
            System.out.println("目标文件所在目录不存在,准备创建它");
            if (!file2.getParentFile().mkdirs())
            {
                System.out.println("复制文件失败,创建目标所在的目录是失败");
                return false;
            }
        }
        int byteread = 0;
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(file);
            out = new FileOutputStream(file2);
            byte[] buffer = new byte[1024];
            while ((byteread = in.read(buffer)) != -1)//in.read(buffer)读取的文件流放在buffer中若文件没有读完则返回字节流的大小   最大为buffer数组的大小  ,若读到文件末尾,则返回-1;
            {
                out.write(buffer, 0, byteread);
            }
            System.out.println("复制单个文件" + srcFileName + "至" + destFileName + "成功");
            return true;
        } catch (IOException e)
        {
            System.out.println("复制文件失败 " + e.getMessage());
            return false;
        } finally
        {
            if (out != null)
            {
                try
                {
                    out.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (in != null)
            {
                try
                {
                    in.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
 
 
 
 
 
    //复制整个目录的内容如果目录存在则不覆盖
    public static boolean copyDirtory(String srcDirName,String destDirName)
    {
        return copyFile(srcDirName, destDirName, false);
    }
 
    public static boolean copyDirtory(String srcDirName,String destDirName,boolean overlay)
    {
        File srcdirFile=new File(srcDirName);
        if(!srcdirFile.exists())
        {
            System.out.println("复制目录失败,源目录"+srcDirName+"不存在");
            return false;
        }
        else if(!srcdirFile.isDirectory())
        {
            System.out.println("复制目录失败,"+srcDirName+"不是一个目录");
            return false;
        }
 
        if(! destDirName.endsWith(File.separator))
            destDirName+=File.separator;
        File destDir=new File(destDirName);
        if(destDir.exists())
        {
            if(overlay)
            {
                System.out.println("目标已存在准备删除它");
                if(!DeletFile.deleteDirectroy(destDirName))
                {
                    System.out.println("复制文件失败,删除原有目录失败");
                    return false;
                }
            }
            else {
                System.out.println("目标已经存在");
            }
        }
        boolean b=true;
        File[]files=destDir.listFiles();
        for(int i=0;i<files.length;i++)
        {
            if(files[i].isFile())
            {
                b=CopyFile.copyFile(files[i].getAbsolutePath(), destDirName+files[i].getName());
                if(b==false)break;
            }
            if(files[i].isDirectory())
            {
                b=copyDirtory(files[i].getAbsolutePath(), destDirName+files[i].getName());
                if(b==false)
                    break;
            }
        }
        if(b==false)
        {
            System.out.println("目录复制失败");
            return false;
        }
        System.out.println("目录复制成功");
        return true;
 
    }
 
    public static void main(String []args)
    {
        String yuan="C:/temp/newTemp.doc";
        String muDiString="C:/haha/ttttt/temp.doc";
        CopyFile.copyFile(yuan, muDiString,true);
        CopyFile.copyDirtory(yuan, muDiString);
    }    
}
posted @ 2016-07-27 10:11  -梦里不知身是客  阅读(160)  评论(0编辑  收藏  举报