正在回答
2回答
同学你好!
如果要拷贝文件夹,同学需要判断目录是否存在,不存在要创建,文件不存在,创建文件,并拷贝文件内容,需要用到遍历和递归,老师这里写了一个小例子,同学可以参考学习一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /* 将"E:/DTLFolder/ceshi"下的所有数据复制到"E:/DTLFolder/ceshi1"下 */ import java.io.*; public class ForDemo{ final static String SOURCESTRING = "E:/DTLFolder/ceshi" ; final static String TARGETSTRING = "E:/DTLFolder/ceshi1" ; public static void main(String[] args){ if (!( new File(SOURCESTRING)).exists()){ System.out.println( "源文件" + SOURCESTRING + "不存在,无法复制!" ); return ; } else { if (( new File(SOURCESTRING)).isFile()){ copyFile( new File(SOURCESTRING), new File(TARGETSTRING)); } else if (( new File(SOURCESTRING)).isDirectory()){ copyDirectory(SOURCESTRING,TARGETSTRING); } } } private static void copyFile(File sourceFile,File targetFile){ if (!sourceFile.canRead()){ System.out.println( "源文件" + sourceFile.getAbsolutePath() + "不可读,无法复制!" ); return ; } else { System.out.println( "开始复制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath()); FileInputStream fis = null ; BufferedInputStream bis = null ; FileOutputStream fos = null ; BufferedOutputStream bos = null ; try { fis = new FileInputStream(sourceFile); bis = new BufferedInputStream(fis); fos = new FileOutputStream(targetFile); bos = new BufferedOutputStream(fos); int len = 0 ; while ((len = bis.read()) != - 1 ){ bos.write(len); } bos.flush(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } finally { try { if (fis != null ){ fis.close(); } if (bis != null ){ bis.close(); } if (fos != null ){ fos.close(); } if (bos != null ){ bos.close(); } System.out.println( "文件" + sourceFile.getAbsolutePath() + "复制到" + targetFile.getAbsolutePath() + "完成" ); } catch (IOException e){ e.printStackTrace(); } } } } private static void copyDirectory(String sourcePathString,String targetPathString){ if (! new File(sourcePathString).canRead()){ System.out.println( "源文件夹" + sourcePathString + "不可读,无法复制!" ); return ; } else { ( new File(targetPathString)).mkdirs(); System.out.println( "开始复制文件夹" + sourcePathString + "到" + targetPathString); File[] files = new File(sourcePathString).listFiles(); for ( int i = 0 ; i < files.length; i++){ if (files[i].isFile()){ copyFile( new File(sourcePathString + File.separator + files[i].getName()), new File(targetPathString + File.separator + files[i].getName())); } else if (files[i].isDirectory()){ copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName()); } } System.out.println( "复制文件夹" + sourcePathString + "到" + targetPathString + "结束" ); } } } |
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
好帮手慕柯南
2019-08-10 16:34:14
同学你好!
如果只是拷贝一个目录,比较简单
在原地址中取出目录的名字,然后在复制的地址中创建一个目录即可
但是通常目录下面会有目录以及文件,此时就会比较复杂:
1.首先判断原地址中的内容是文件还是目录,如果是目录在目标地址中创建一个目录。此时仍然需要继续遍历当前目录下面是否还有目录,直到目录下只有文件,没有文件夹
2.如果是文件,首先在目标地址相应的目录中创建文件,然后在进行文件内容的复制
这种就是老师代码中写的这种。同学现在理解可能会有点困难。建议同学在多积累点知识在回过头来看这个问题,会比较容易理解。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧