1、对拷贝源的遍历代码 package com.javase.io.homework; import java.io.File; /* 该程序的所有代码我们不可能一次性完成到位,只有分步进行,一点一点增加要实现的功能代码,我们首先来完成拷贝源的目录和文件的遍历,把目录中的内容一一找到,找到后根据内容该拷贝就拷贝,该新建就新建。也就是说找到的是文件就拷贝,找到的是目录就新建 */ public class HomeWork { public static void main(String[] args) { //拷贝源 File srcFile = new File("D:\\2345Downloads"); //拷贝目标 File destFile = new File("C:\\"); //调用拷贝方法完成拷贝 copyDir(srcFile,destFile); }
/** * 目录拷贝方法 * @param srcFile 拷贝源 * @param destFile 拷贝目标 */ private static void copyDir(File srcFile, File destFile) { if (srcFile.isFile()){ //这里使用FileInputStream + FileOutputStream完成文件的拷贝. return; } File[] files = srcFile.listFiles(); for (File file:files) { //这里完成目录的创建 System.out.println(file.getAbsolutePath()); //递归调用 copyDir(file,destFile); } } }
---------------------------------------------------------------------------------------------------------------------------------
2、完成拷贝的代码
package com.javase.io.homework; import java.io.*; import java.util.Scanner; /** * 目录拷贝(完成对某个目录下的子目录和文件的拷贝工作,多少层目录结构不限) */ public class HomeWork { public static void main(String[] args) { // 创建键盘扫描仪对象 Scanner scanner = new Scanner(System.in); System.out.print("请输入拷贝源:"); String s1 = scanner.next(); // 拷贝源 File srcFile = new File(s1); System.out.print("请输入拷贝目标:"); String s2 = scanner.next(); // 拷贝目标 File destFile = new File(s2); // 调用拷贝方法 开始拷贝 copyDir(srcFile,destFile); } /** * 目录拷贝方法 * @param srcFile 拷贝源 * @param destFile 拷贝目标 */ private static void copyDir(File srcFile, File destFile) { //如果srcFile是文件,就对该文件进行拷贝 if (srcFile.isFile()){ //需要拷贝文件 //定义字节输入流和字节输出流对象 FileInputStream in = null; FileOutputStream out = null; try { //创建字节输入流对象 in = new FileInputStream(srcFile); //获取拷贝目标路径(通过字符串的拼接来完成) /* destFile.getAbsolutePath() 是获取拷贝目标的绝对路径; destFile.getAbsolutePath().endsWith("\\") 是用来判断获取得到的绝对路径是否以"\"结尾 destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath():destFile.getAbsolutePath() + "\\" 上行代码是三目运算符表达式,它表示:获取得到的绝对路径如果不是以"\"结尾,就在后面加上"\"。 srcFile.getAbsolutePath() 获取拷贝源的绝对路径 srcFile.getAbsolutePath().substring(3) 对获取的拷贝源绝对路径进行字符串的截取操作,去掉前三个字符。 然后通过"+"对字符串进行拼接,得到一个新字符串作为拷贝目标的路径 */ String past = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath():destFile.getAbsolutePath() + "\\") + srcFile.getAbsolutePath().substring(3); //创建字节输出流对象 out = new FileOutputStream(past); //创建一个byte数组,拷贝文件用 byte[] bytes = new byte[1024 * 1024];//每次读取1M字节。 int byteCount = 0; while ((byteCount = in.read(bytes)) != -1){ out.write(bytes,0,byteCount); } //刷新输出流 out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭输出流 if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } //关闭输入流 if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return;//结束最近一次调用copyDir.或者说是结束最近一次递归调用。 } //获取拷贝源下面的所有子File File[] files = srcFile.listFiles(); //遍历 //采用foreach循环 for (File file:files) { if (file.isDirectory()){ //获取File对象的绝对路径,并截取字符串(将"D:\"截掉,应该使用substring(3)。) String srcDir = file.getAbsolutePath().substring(3); //获取拷贝目标路径 String destDir = destFile.getAbsolutePath(); //通过字符串拼接得到目标路径 String past = (destDir.endsWith("\\")? destDir : destDir + "\\") + srcDir; File newFile = new File(past); if (!newFile.exists()){ newFile.mkdirs(); } } //递归调用 copyDir(file,destFile); } /* //采用普通for循环 for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()){ //获取File对象的绝对路径,并截取字符串(将"D:\"截掉,应该使用substring(3)。) String srcDir = files[i].getAbsolutePath().substring(3); //获取拷贝目标路径 String destDir = destFile.getAbsolutePath(); //通过字符串拼接得到目标路径 String past = (destDir.endsWith("\\")? destDir : destDir + "\\") + srcDir; File newFile = new File(past); if (!newFile.exists()){ newFile.mkdirs(); } } // copyDir(files[i],destFile); } */ } }