zoukankan      html  css  js  c++  java
  • 把C盘中的一个多层文件夹拷贝到D盘中 黑马程序员

    ---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
    
    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();
            }
    
    }
    
    
    ---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
  • 相关阅读:
    JAVA获取昨天、今天、明天等日期
    IDEA设置调用方法时提示方法上的注释
    Hibernate使用distinct返回不重复的数据,使用group by 进行分组
    SpringBoot 自定义注解
    tailwindcss 使用总结
    nodejs nvm 包管理
    macos NPM 全局安装解决方案
    git 遇到修改github密码导致本地push失败解决方案
    Jupyter 快捷方式设置
    Vue indent eslint缩进webstorm冲突解决
  • 原文地址:https://www.cnblogs.com/gaopeng781/p/4334578.html
Copyright © 2011-2022 走看看