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(); } }
把c盘的文件夹拷贝到d盘中 黑马程序员
内容版权声明:除非注明,否则皆为本站原创文章。