楼主的代码太混乱了,我只是放了一个输出语句在22行,结果就执行不到。就再没往下看了。。贴上一个更加简练的代码
- package day4;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- //复制制定指定文件夹到指定位置
- public class CopyFileAndDir {
- public static void main(String[] args) {
- File srcDir = new File("F:\\其他\\PPT主题\\图片素材");//源文件夹
- File descDir = new File("F:\\hello2"); //复制后存放的文件夹
-
- //如果这个文件夹不存在,则创建
- if(!descDir.exists())
- descDir.mkdir();
-
- copy(srcDir,descDir); //开始copy
- }
- public static void copy(File file,File mudi)
- {
- File[] files = file.listFiles();//获取制定位置全部列表
- for (File file2 : files) {
- if(file2.isDirectory())
- {
- File dir = new File(file2.toString());
- File mk = new File(mudi+File.separator+dir.getName());
- mk.mkdir();
- copy(dir,mk);
- }else if(file2.isFile())
- {
- String src = file2.getAbsolutePath();
- String dec =mudi+File.separator+file2.getName();
- copyFile(src,dec);
- }
- }
- }
- //复制文件
- public static void copyFile(String fileName,String copyName)
- {
- File file = new File(fileName);//源文件
- File copyfile = new File(copyName);//存放位置及文件名
- BufferedInputStream bis =null;
- BufferedOutputStream bos = null;
- try {
- bis = new BufferedInputStream(new FileInputStream(file));
- bos = new BufferedOutputStream(new FileOutputStream(copyfile));
- //这里不用多说了吧
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = bis.read(buf)) > 0)
- {
- bos.write(buf, 0, len);
- }
-
- } catch (Exception e) {
- System.out.println("读写文件失败");
- }finally
- {
- try {
- if(bis != null)
- {
- bis.close();
- }
- } catch (Exception e2) {
- System.out.println("缓冲区读取流关闭失败");
- }
- try {
- if(bos != null)
- {
- bos.close();
- }
- } catch (Exception e2) {
- System.out.println("缓冲区输出流关闭失败");
- }
- }
- }
- }
复制代码
|