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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© major2015 中级黑马   /  2015-4-28 09:56  /  571 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

不多说,上代码(;;怕删帖,你懂得:D)
下面还有些地方需要修饰,思路就是这样了,,,焦虑不安啊
  1. /*
  2. *
  3. * 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
  4. *
  5. */
  6. public class Test2 {

  7.         public static void main(String[] args) throws IOException {
  8.                         String strFile="srcfile";
  9.                         String desFile="desfile";
  10.                         copyFile(strFile,desFile);
  11.         }

  12.         public static void copyFile(String srcFile, String desFile)
  13.                         throws IOException {
  14.                 File src = new File(srcFile);
  15.                 File des = new File(desFile);

  16.                 if (!(src.exists())) {
  17.                         throw new RuntimeException("文件夹不存在");
  18.                 }

  19.                 if (!des.exists())
  20.                         des.mkdir();
  21.                 File[] files =src.listFiles(new FileFilter() {

  22.                         @Override
  23.                         public boolean accept(File pathname) {

  24.                                 if (pathname.toString().endsWith(".java"))
  25.                                         return true;
  26.                                 return false;
  27.                         }
  28.                 });
  29.                 System.out.println(Arrays.toString(files));
  30.                 for (int i = 0; i < files.length; i++) {
  31.                         BufferedReader br = new BufferedReader(new FileReader(files[i]));
  32.                        
  33.                         String name = files[i].getName().replace(".java", ".txt");
  34.                         String line = null;
  35.                         File temp = new File(des,name);
  36.                         PrintWriter pw = new PrintWriter(temp);

  37.                         while ((line = br.readLine()) != null) {
  38.                                 pw.println(line);
  39.                         }
  40.                         System.out.println(temp+"复制完成!");
  41.                         pw.flush();
  42.                         br.close();
  43.                         pw.close();
  44.                 }

  45.         }
  46. }
复制代码



5 个回复

倒序浏览
感谢分享,貌似还需要导入这些包:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
回复 使用道具 举报
牛!!!
回复 使用道具 举报
学习了。。。。
回复 使用道具 举报
要学习。
回复 使用道具 举报
这个是干什么用的呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马