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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© HM何伟 中级黑马   /  2013-4-21 19:13  /  1319 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 HM何伟 于 2013-4-21 19:15 编辑

不知道错在那里?求指点

package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CopyDirectory {

        public static void main(String[] args) throws IOException {
                File src = new File("d:\\src");

                File target = new File("e:\\");

                CopyDir(src, target);

        }

        // 复制目录
        public static void CopyDir(File src, File target) throws IOException {
                if (src.isDirectory()) {
                        File dir = new File(target, src.getName());
                        dir.mkdirs();
                        File[] filesrc = src.listFiles();
                        for (File file : filesrc) {
                                CopyDir(file, dir);
                        }
                } else {

                        File file = new File(target, src.getName());
                        file.createNewFile();
                        CopyFile(file, target);
                }
        }

        // 复制文件
        private static void CopyFile(File src, File target) throws IOException {

                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                                src));

                BufferedOutputStream bos = new BufferedOutputStream(
                                new FileOutputStream(target));
                byte[] bys = new byte[1024];
                int len = 0;
                while ((len = bis.read(bys)) != -1) {
                        bos.write(bys);
                        bos.flush();
                }
                bos.close();
                bis.close();

        }

}


2 个回复

正序浏览
何俊森 发表于 2013-4-22 20:04
lz复制文件的时候,传递参数错误  CopyFile(src, file)。

谢了,已经好了
回复 使用道具 举报
lz复制文件的时候,传递参数错误  CopyFile(src, file)。
  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 CopyDirectory {

  8.         public static void main(String[] args) throws IOException {
  9.                 File src = new File("d:\\src");

  10.                 File target = new File("e:\\");

  11.                 CopyDir(src, target);

  12.         }

  13.         // 复制目录
  14.         public static void CopyDir(File src, File target) throws IOException {
  15.                 if (src.isDirectory()) {
  16.                         File dir = new File(target, src.getName());
  17.                         dir.mkdirs();
  18.                         File[] filesrc = src.listFiles();
  19.                         for (File file : filesrc) {
  20.                                 CopyDir(file, dir);
  21.                         }
  22.                 } else {

  23.                         File file = new File(target, src.getName());
  24.                         file.createNewFile();
  25.                         CopyFile(src, file);//这里面按照private static void CopyFile(File src, File target)应该是这样。
  26.                 }
  27.         }

  28.         // 复制文件
  29.         private static void CopyFile(File src, File target) throws IOException {

  30.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
  31.                                 src));

  32.                 BufferedOutputStream bos = new BufferedOutputStream(
  33.                                 new FileOutputStream(target));
  34.                 byte[] bys = new byte[1024];
  35.                 int len = 0;
  36.                 while ((len = bis.read(bys)) != -1) {
  37.                         bos.write(bys);
  38.                         bos.flush();
  39.                 }
  40.                 bos.close();
  41.                 bis.close();

  42.         }

  43. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马