哥们你的代码太..... 建议以后贴的容易复制点呀{:soso_e127:}
我整理了好久,还是给你改了下
- import java.io.*;
- class MainClass {
- public static void main(String[] args) throws Exception
- {
- //假设你把E盘下“aaa”这个文件夹给拷贝到E盘并命名为copyfile
- findAndCopyPics("E:\\aaa",
- "E:\\copyfile");
- }
- public static void findAndCopyPics(String fpath,String dpath) throws Exception
- {
- File file = new File(fpath);
- if (file.isDirectory())
- {
- File copyfile = new File(dpath);
- copyfile.mkdirs();
- }
- File[] listFiles = file.listFiles();
- for (File f : listFiles) {
- if (f.isDirectory())
- {
- // 是文件夹就递归
- findAndCopyPics(fpath + "\\" + f.getName(),dpath+"\\"+f.getName());
- }
- else
- {
- // 不是文件夹,即是文件就拷备
- copyPics(fpath + "\\" + f.getName(),dpath+"\\"+f.getName());
- }
- }
- }
- public static void copyPics(String fpath,String dpath) throws Exception
- {
- //File inputfile = new File(fpath);
- //File outputfile = new File(dpath);// 输出的文件夹.
- //System.out.println(path);
- //FileInputStream fis = new FileInputStream(inputfile);
- FileInputStream fis = new FileInputStream(fpath);
- //FileOutputStream fos = new FileOutputStream(outputfile);
- FileOutputStream fos = new FileOutputStream(dpath);
- byte[] b = new byte[1024];
- int len = 0;
- while ((len=fis.read(b))!=-1)
- {
- fos.write(b,0,len);
- }
- fis.close();
- fos.close();
- }
- }
复制代码
有的地方我改了,把你的源码注释了 |