• 把c盘的文件夹拷贝到d盘中 黑马程序员


    ---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

    class CopyWenJianJia
    {
            public static void main(String[] args)throws Exception
            {
                    //源文件夹
                    String yuan = "C:\nimeizide";
                    //目的地
                    String mudi = "D:\nimeizide";
                    //建立目标文件夹
                    (new File(mudi)).mkdirs();
                    //获取源文件夹当下的文件或目录
                    File[] files = (new File(yuan)).listFiles();
                    for(File file : files)
                    {
                            if(file.isDirectory())
                            {
                                    String yuanDir = yuan + "\" + file.getName();
                                    String mudiDir = mudi + "\" + file.getName();
                                    //复制目录
                                    copyDir(yuanDir, mudiDir);
                            }
                            else
                            {
                                    copyFile(file, new File(mudi + "\" + file.getName()));
                            }
                    }
    
            }
            //复制文件夹
            public static void copyDir(String yuanDir, String mudiDir)throws Exception
            {
                    (new File(mudiDir)).mkdirs();
                    File[] files = (new File(yuanDir)).listFiles();
                    for(File file : files)
                    {
                            if(file.isFile())
                            {
                                    File yuanFile = file;//源文件
                                    File mudiFile = new File(new File(mudiDir).getAbsolutePath() + "\" + file.getName());
                                    copyFile(yuanFile, mudiFile);
                            }
                            else
                            {
                                    String yuanJia = yuanDir + "\" + file.getName();
                                    String mudiJia = mudiDir + "\" + file.getName();
                                    copyDir(yuanJia, mudiJia);
                            }
                    }
            }
            //复制文件
            public static void copyFile(File yuanFile, File mudiFile)throws Exception
            {
                    BufferedInputStream buis = new BufferedInputStream(new FileInputStream(yuanFile.getAbsoluteFile()));
                    BufferedOutputStream buos = new BufferedOutputStream(new FileOutputStream(mudiFile.getAbsoluteFile()));
                    byte[] buf = new byte[1024];
                    int len;
                    while((len = buis.read(buf)) != -1)
                    {
                            buos.write(buf, 0, len);
                            buos.flush();
                    }
                    buis.close();
                    buos.close();
            }
    
    }

    ---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

  • 相关阅读:
    24. Swap Nodes in Pairs
    2. Add Two Numbers
    【设计模式】结构型模式
    【设计模式】创建型模式
    【设计模式】初识
    【自考总结】走过的弯路,都是你成长的旅途
    【VMware vSphere】再谈VMware vSphere
    评估网站性能的专业术语
    C/S与B/S之辩
    【VMware vSphere】Veeam备份
  • 原文地址:https://www.cnblogs.com/gaopeng781/p/4335548.html
Copyright © 2020-2023  润新知