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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© skill20 中级黑马   /  2014-5-8 20:17  /  1457 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 需求:复制文件夹下的文件。
  3. 分析:用读取流关联文件,建立文件夹对象。判断是否是目录。
  4. */
  5. import java.io.*;
  6. import java.util.*;
  7. class  CopyTest
  8. {
  9.         public static void main(String[] args) throws IOException
  10.         {
  11.                 copy();
  12.         }
  13.         public static void copy()throws IOException
  14.         {
  15.                 File dir1 = new File("d:\\ab");
  16.                 File dir2 = new File("e:\\at");
  17.                 if(!dir2.exists())
  18.                         dir2.mkdir();
  19.                 List<File> list = new ArrayList<File>();
  20.                 get(dir1,list);
  21.                 for(File files : list)
  22.                 {
  23.                         BufferedReader bfr
  24.                                 = new BufferedReader(new FileReader(files));//读取文件。
  25.                         BufferedWriter bfw
  26.                                 = new BufferedWriter(new FileWriter( dir2 + "\\"+ files.getName()));
  27.                         String str = null;
  28.                         while((str = bfr.readLine()) != null)//写文件。       
  29.                         {
  30.                                 bfw.write(str);
  31.                                 bfw.newLine();
  32.                                 bfw.flush();
  33.                         }
  34.                         bfw.close();
  35.                         bfr.close();
  36.                 }
  37.         }
  38.         public static void get(File dir, List<File> list)//把文件全部存到集合里。
  39.         {
  40.                 File[] files = dir.listFiles();
  41.                 for( int x = 0; x < files.length; x++)
  42.                 {
  43.                         if(files[x].isDirectory())
  44.                                 get(files[x],list);
  45.                         else
  46.                                 list.add(files[x]);
  47.                
  48.                 }
  49.         }
  50. }
复制代码
目录文件夹下文件的复制这个代码可行不?

3 个回复

倒序浏览
你这样复制,文件夹下的子目录不就没了,目标文件夹里全部都是文件,没有层次之分,不如不复制了
回复 使用道具 举报
曹冬明 发表于 2014-5-8 20:31
你这样复制,文件夹下的子目录不就没了,目标文件夹里全部都是文件,没有层次之分,不如不复制了 ...

恩,这是个问题,那要咋搞?
回复 使用道具 举报
我这有个垃圾的列子 你看下:package itheima.com.filer;
import java.io.*;

public class CopyFile {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                String path="D:\\apache-tomcat-7.0.33";
                File file = new File(path);
                searchFile(file);

        }
//递归并复制文件
        private static void searchFile(File file) throws IOException {
                File[] files = file.listFiles();
                for (File sFile : files) {
                        if (sFile.isDirectory()) {
//递归
                                searchFile(sFile);
                        } else {
//F盘下文件目录
                                String newName = "F" + sFile.getPath().substring(1);

                                String dir=newName.substring(0, newName.lastIndexOf("\\"));
                                String FileName =sFile.getName();
                                File pathFile =new File(dir);
                                pathFile.mkdirs();
//F盘下目录和文件名
                                File newFile =new File (pathFile,FileName);
//复制文件
                                copyFileAll(sFile, newFile);
                        }

                }

        }
//复制文件
        private static void copyFileAll(File oldFile, File newFile)
                        throws IOException {
                BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(oldFile)));
                BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile)));
                String line=null;
                while ((line = br.readLine()) != null) {
                        bw.write(line);
                        bw.newLine();
                       
                        bw.flush();

                }
                br.close();
                bw.close();
       
        }

}
这个代码不是很好 希望能帮到你

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

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