zoukankan      html  css  js  c++  java
  • 把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培训、期待与您交流! ----------------------

  • 相关阅读:
    Codeforces 691A Fashion in Berland
    HDU 5741 Helter Skelter
    HDU 5735 Born Slippy
    HDU 5739 Fantasia
    HDU 5738 Eureka
    HDU 5734 Acperience
    HDU 5742 It's All In The Mind
    POJ Euro Efficiency 1252
    AtCoder Beginner Contest 067 C
    AtCoder Beginner Contest 067 D
  • 原文地址:https://www.cnblogs.com/gaopeng781/p/4335548.html
Copyright © 2011-2022 走看看