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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

刘吉庆

中级黑马

  • 黑马币:0

  • 帖子:37

  • 精华:0

© 刘吉庆 中级黑马   /  2013-4-7 22:42  /  1594 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘吉庆 于 2013-4-11 22:01 编辑

如题,这是我写的拷贝文件并修改其后缀的代码,想抽取成三个方法,但想了半天也想不出来,高手帮我看看,谢谢
1. 遍历目录
2. 修改后缀
3. 拷贝文件
  1. public class CopyAndEditFilesName {

  2.         /**
  3.          * @param args
  4.          * @throws IOException
  5.          * @throws FileNotFoundException
  6.          */
  7.         public static void main(String[] args) throws FileNotFoundException, IOException {
  8.                 File pathName = new File("d:\\java");
  9.                 listFiles(pathName);
  10.         

  11.         }
  12. //        遍历目录
  13.         public static void listFiles(File file) throws FileNotFoundException,IOException {
  14.                 File fileArray[] = file.listFiles();
  15.                 for (File f : fileArray) {
  16.                         if (f.isDirectory()) {
  17.                                 listFiles(f);
  18.                         } else {
  19.                                 if (f.getName().endsWith(".java")) {
  20.                                         //构造新的文件parent目录
  21.                                         String parentPath = f.getParent().replaceAll("^d:\\\\java", "d:\\\\jad");
  22.                                         File copyPath = new File(parentPath);
  23.                                         // 把.java类型的文件改为.jad
  24.                                         String newFileName = f.getName().replaceAll("\\.java[        DISCUZ_CODE_0        ]quot;,".jad");
  25.                                         if(!copyPath.exists()) {
  26.                                                 copyPath.mkdir();
  27.                                         }
  28.                                         File newFile = new File(parentPath, newFileName);
  29.                                         // 调用文件拷贝方法
  30.                                         copyFile(new FileReader(f), new FileWriter(newFile));
  31.                                 }
  32.                         }
  33.                 }
  34.         }
  35. //        文件拷贝
  36.         public static void copyFile(FileReader fr, FileWriter fw) {
  37.                 BufferedReader br = new BufferedReader(fr);
  38.                 BufferedWriter bw = new BufferedWriter(fw);
  39.                 String str;
  40.                 try {
  41.                         while((str = br.readLine()) != null) {
  42.                                 fw.write(str);
  43.                         }
  44.                 } catch (IOException e) {
  45.                         e.printStackTrace();
  46.                 } finally {
  47.                         if(bw != null) {
  48.                                 try {
  49.                                         bw.close();
  50.                                 } catch (IOException e) {
  51.                                         e.printStackTrace();
  52.                                 }
  53.                         }
  54.                         if(br != null) {
  55.                                 try {
  56.                                         br.close();
  57.                                 } catch (IOException e) {
  58.                                         e.printStackTrace();
  59.                                 }
  60.                         }
  61.                 }
  62.         }
  63. }
复制代码

评分

参与人数 2技术分 +2 收起 理由
滔哥 + 1 多送1分,论坛参与奖。
陈丽莉 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 黑马_位志国 于 2013-4-8 21:10 编辑

import java.io.*;
import java.util.*;
//将d:/java目录下的所有java文件,移动到d:/jad目录下,并将java文件改为jad文件
public class Test {
        private static ArrayList<File> al = new ArrayList<File>();
        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub

                File f = new File("d:/java");
                moveFile(f);
        }
        
        //拷贝,并修改文件类型后,将文件存储到d:/jad目录下
        public static void moveFile(File srcFile) {
                listFiles(srcFile);
                File f = new File("d:/jad");
               
                if(!f.isDirectory()){
                        f.mkdir();
                }
               
                for(File file : al){
                        copyFile(file, new File(f, file.getName().replaceAll(".java", ".jad")));
                }
        }
        
        //拷贝文件,通过缓冲技术来提高缓冲效率
        public static void copyFile(File src, File dest) {
                BufferedReader bufr = null;
                BufferedWriter bufw = null;
                try {
                        bufr = new BufferedReader(new FileReader(src));
                        bufw = new BufferedWriter(new FileWriter(dest));
                        
                        String line = null;
                        
                        while((line = bufr.readLine()) != null){
                                bufw.write(line);
                                bufw.newLine();
                                bufw.flush();
                        }
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        throw new RuntimeException("源文件未找到");
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        throw new RuntimeException("移动或更改文件失败");
                }finally{
                        if(bufw != null){
                                try {
                                        bufw.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        throw new RuntimeException("资源关闭失败");
                                }
                        }
                        
                        if(bufr != null){
                                try {
                                        bufr.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        throw new RuntimeException("资源关闭失败");
                                }
                        }
                }

        }
        
        //遍历目录
        public static ArrayList<File> listFiles(File srcFile){
                //获取该目录下的所有文件
                File[] files = srcFile.listFiles();
               
                for(File file : files){
                        if(file.isDirectory()){
                                listFiles(file);
                        }else
                                //如果是java文件,就将该文件保存起来
                                if(file.getName().endsWith(".java")){
                                        al.add(file);
                                }
                }
               
                return al;
        }

}

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 恭喜25分了。

查看全部评分

回复 使用道具 举报
谢谢滔哥的鼓励,也谢谢楼上的写的代码的思路:
但你代码中replaceAll稍有出入,如文件名为.java就会出错
回复 使用道具 举报
  1. public static void main(String[] args) throws IOException {

  2.                 File originFile = new File("d:\\java");//源目录
  3.                 String destParent = "d:\\jad";//目标目录
  4.                 String replace = ".jad";//更改为的类型名
  5.                 File destFile = new File(destParent);//构造目标目录文件
  6.                 if (!destFile.exists()) {
  7.                         destFile.mkdir();//创建
  8.                 }
  9.                 // 获取目录下的所有类型为.java的文件并存在typeFiles集合中
  10.                 ArrayList<File> typeFiles = new ArrayList<File>();
  11.                 ArrayList<File> originFileList = listFiles(originFile,typeFiles);
  12.                 // 遍历集合
  13.                 Iterator<File> it = originFileList.iterator();
  14.                 while (it.hasNext()) {
  15.                         File origin = it.next();
  16.                         FileReader fr = new FileReader(origin);
  17.                         String fileName = editFile(origin, replace).getName();//修改后的文件名
  18.                         // 用目标文件和改后的文件名构造文件,构造字符输出流
  19.                         FileWriter fw = new FileWriter(new File(destParent, fileName));
  20.                         // 拷贝文件
  21.                         copyFile(fr, fw);
  22.                 }

  23.         }

  24.         /**
  25.          * 修改文件类型
  26.          *
  27.          * @param files     要修改的目录集合
  28.          * @param replace   修改类型名称(以.开始,如.jad)
  29.          * @return          目录集合
  30.          */
  31.         public static ArrayList<File> editFiles(ArrayList<File> files, String replace) {
  32.                 ArrayList<File> fileList = new ArrayList<File>();
  33.                 Iterator<File> it = files.iterator();
  34.                 while (it.hasNext()) {
  35.                         String fileName = it.next().getName().replaceAll("\\.java$", replace);
  36.                         fileList.add(new File(fileName));
  37.                 }
  38.                 return fileList;

  39.         }
  40.         /**
  41.          * 修改文件类型
  42.          * @param file      要修改的文件
  43.          * @param replace   替换的类型名称(以.开始,如.jad)
  44.          * @return          修改后的文件
  45.          */
  46.         public static File editFile(File file, String replace) {
  47.                 return new File(file.getName().replaceAll("\\.java$", replace));
  48.         }

  49.         // 拷贝文件,通过缓冲技术来提高缓冲效率
  50.         public static void copyFile(FileReader fr, FileWriter fw) {
  51.                 BufferedReader br = new BufferedReader(fr);
  52.                 BufferedWriter bw = new BufferedWriter(fw);
  53.                 String str;
  54.                 try {
  55.                         while ((str = br.readLine()) != null) {
  56.                                 bw.write(str);// 写出一行
  57.                                 bw.newLine();// 换行
  58.                                 bw.flush();// 刷新
  59.                         }
  60.                 } catch (IOException e) {
  61.                         throw new RuntimeException("IO异常!");
  62.                 } finally {
  63.                         if (bw != null) {
  64.                                 try {
  65.                                         bw.close();
  66.                                 } catch (Exception e1) {
  67.                                         throw new RuntimeException("字节缓冲输出流未关闭!!");
  68.                                 }
  69.                         }
  70.                         if (br != null) {
  71.                                 try {

  72.                                 } catch (Exception e2) {
  73.                                         throw new RuntimeException("字节缓冲输入流未关闭!!");
  74.                                 }
  75.                         }
  76.                 }
  77.         }
  78.         /**
  79.          * 遍历目录
  80.          * @param originFile    要遍历的目录
  81.          * @param typeFiles     存储符合条件目录的集合
  82.          * @return
  83.          */
  84.         public static ArrayList<File> listFiles(File originFile, ArrayList<File> typeFiles) {
  85.                 // 获取源目录下的所有文件
  86.                 File[] files = originFile.listFiles();
  87.                 // 遍历
  88.                 for (File file : files) {
  89.                         if (file.isDirectory()) {
  90.                                 listFiles(file, typeFiles);
  91.                         } else {
  92.                                 // 添加.java结尾的文件到目录集合
  93.                                 if (file.getName().endsWith(".java")) {
  94.                                         typeFiles.add(file);
  95.                                 }
  96.                         }
  97.                 }
  98.                 return typeFiles;
  99.         }
复制代码
最终代码,想得难免有所不周,贴出来还请指正

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