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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Gyx 中级黑马   /  2015-1-3 23:35  /  1507 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt

2 个回复

倒序浏览
小样例,看看是不是你想要的!
  1. /*
  2.         需求:文件复制
  3.         分析:
  4.         1.明确要操作的是否是文本文件,如果是用FileReader
  5.         2.明确目的文件是否是文本问价,如果是使用FileWriter
  6.         3.明确是不是要提高效率,如果是,使用缓冲区
  7. */
  8. import java.io.*;

  9. class CopyDemo
  10. {
  11.         public static void main(String[] args)throws Exception
  12.         {
  13.                 BufferedReader bufr =
  14.                         new BufferedReader(new FileReader("ReflectDemo.java"));
  15.                 BufferedWriter bufw =
  16.                         new BufferedWriter(new FileWriter("test.txt"));
  17.                 String line = null;
  18.                 while((line = bufr.readLine()) != null)
  19.                 {
  20.                         bufw.write(line);
  21.                         bufw.newLine();
  22.                         bufw.flush();               
  23.                 }
  24.                 bufw.close();
  25.                 System.out.println("Copy 完成!请检查!");
  26.         }
  27. }
复制代码
回复 使用道具 举报
这个主要是考的递归和IO流,直到怎么遍历文件夹的所有文件就差不多了。剩下的就是思路了。
  1. /*将d:\\下的所有java文件拷贝到d:\\jad目录下后缀名为.txt
  2. * 思路:要获取到d:\\下的所有java文件的绝对路径并封装为文件对象,
  3. * 建立文件读取流关联文件,建立文件写入流,将文件复制到制定目录。
  4. *
  5. * 步骤: 1.遍历d:\\ 目录,找出.java文件,文件路径存放到一个文件中
  6. *              2.读取路径,建立路径文件,关联读取流,复制文件,并将名字改为.txt
  7. * */
  8. import java.io.*;
  9. public class CopyFiles {

  10.         public static void main(String[] args) throws IOException {
  11.                 File src=new File("D:\\java\\0924\\day01");
  12.                 File findFilePath=new File("D:/findJavaFile.txt");
  13.                 File newFilePath=new File("D:/copy_JavaFiles");
  14.                 StringBuilder sb=new StringBuilder();
  15.                 findFiles(src,sb);
  16.                 Writer wr=new FileWriter(findFilePath);
  17.                 wr.write(sb.toString());
  18.                 wr.close();
  19.                
  20.                 copyJavaFile(findFilePath,newFilePath);
  21.                
  22.         }
  23.         private static void copyJavaFile(File findFilePath, File newFilePath) throws IOException {
  24.                 if(!newFilePath.exists())
  25.                 {
  26.                         newFilePath.mkdirs();
  27.                 }
  28.                 BufferedReader bufr=new BufferedReader(new FileReader(findFilePath));
  29.                 String line=null;
  30.                 int num=0;
  31.                 while((line=bufr.readLine())!=null)
  32.                 {
  33.                         File f=new File(line);//将每个java存在的路径封装为一个文件对象
  34.                         BufferedReader bufr2=new BufferedReader(new FileReader(f));
  35.                         BufferedWriter bufw=new BufferedWriter(new FileWriter(new File(newFilePath,f.getName().replaceAll("\\.java", "\\.txt"))));
  36.                         String str=null;
  37.                         while((str=bufr2.readLine())!=null)
  38.                         {
  39.                                 bufw.write(str);
  40.                                 bufw.newLine();
  41.                         }
  42.                         bufr2.close();
  43.                         bufw.close();
  44.                         num++;
  45.                 }
  46.                 bufr.close();
  47.                 System.out.println("共计"+num+"个java文件拷贝成功!");
  48.                
  49.         }
  50.         public static void findFiles(File dir,StringBuilder sb)
  51.         {
  52.                
  53.                 File[] files=dir.listFiles();
  54.                
  55.                 for(File f:files)
  56.                 {
  57.                         if(f.isDirectory())
  58.                         {
  59.                                 findFiles(f,sb);
  60.                         }
  61.                         else if(f.getName().endsWith(".java"))
  62.                         {
  63.                                 sb.append(f.getAbsolutePath()+"\r\n");
  64.                         }
  65.                 }
  66.                
  67.         }
  68.        

  69. }
复制代码


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