A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Aimer_WJY 中级黑马   /  2014-4-1 13:41  /  820 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class CopyFile {
  2.                         public static void main(String [] args) throws Exception{
  3.                                 File src=new File("F:\\test2");
  4.                                 System.out.println(src.getAbsolutePath());
  5.                                 File des=new File("F:\\copy_test2");
  6.                                 File[] arr=src.listFiles();
  7.                                 for(File a:arr){
  8.                                         if(a.isDirectory())
  9.                                                 copyDirectory(a,src,des);
  10.                                         else
  11.                                                 copyFile(a,src,des);
  12.                                 }
  13.                                
  14.                         }
  15.         public static  void copyFile(File a,File src,File des) throws Exception{
  16.                 //BufferedInputStream buis=new BufferedInputStream(new FileInputStream(src.getAbsolutePath()));
  17.                 FileInputStream fis=new FileInputStream(a.getAbsolutePath());
  18.                 //BufferedOutputStream buos=new BufferedOutputStream(new FileOutputStream(des.getAbsolutePath()));
  19.                 FileOutputStream fos=new FileOutputStream(a.getAbsolutePath());
  20.                 int len=0;
  21.                 while((len=fis.read())!=-1){
  22.                         fos.write(len);
  23.                 }               
  24.         }
  25.         public static void copyDirectory(File f,File src,File des) throws Exception{
  26.                 File name=new File(f.getAbsolutePath(),f.getName());       
  27.                 name.mkdir();
  28.                 File[] arr=src.listFiles();
  29.                 for(File a:arr){
  30.                         if(a.isFile()){
  31.                         copyFile(a,src,des);
  32.                         }
  33.                         else{                               
  34.                                 copyDirectory(a,src,des);
  35.                         }
  36.                 }
  37.                
  38.         }
  39. }
复制代码
错误是:
Exception in thread "main" java.lang.StackOverflowError
        at java.lang.ref.Finalizer.<init>(Finalizer.java:85)
        at java.lang.ref.Finalizer.register(Finalizer.java:90)



目前功能也没用实现,请大神指导下~

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

1 个回复

倒序浏览
看下我之前写的吧
个人觉得还不错

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. public class FileCopyDemo2 {
  8.         public static void main(String[] args) throws IOException {
  9.                 File source = new File("D:\\myjava\\myself\\新建文件夹");
  10.                 File target = new File("D:\\myjava\\test", source.getName());

  11.                
  12.                 if(source.isDirectory())
  13.                         folderCopy(source, target);
  14.                 else
  15.                         fileCopy(source, target);
  16.         }

  17.         public static void folderCopy(File source, File target) throws IOException {
  18.                 if (!target.exists()) {
  19.                         target.mkdir();
  20.                 }
  21.                 File[] files = source.listFiles();

  22.                 for (File file : files) {
  23.                         String newSource = file.getAbsolutePath();
  24.                         String newtarget = newSource.replace(source.getAbsolutePath(),
  25.                                         target.getAbsolutePath());
  26.                         File newFile = new File(newtarget);
  27.                         if (file.isDirectory()) {
  28.                                 folderCopy(file, newFile);
  29.                         }
  30.                         else {
  31.                                 fileCopy(file, newFile);
  32.                         }
  33.                 }
  34.         }

  35.         public static void fileCopy(File file, File newFile) throws IOException {
  36.                 FileInputStream fis = new FileInputStream(file);
  37.                 FileOutputStream fos = new FileOutputStream(newFile);
  38.                
  39.                 BufferedInputStream bfis = new BufferedInputStream(fis);
  40.                 BufferedOutputStream bfos = new BufferedOutputStream(fos);
  41.                
  42.                 int i = 0;
  43.                 while ((i = bfis.read()) != -1) {
  44.                         bfos.write(i);
  45.                 }
  46.                 bfis.close();
  47.                 bfos.close();
  48.         }
  49. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马