编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为. jad

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;

public class CopyFolderTest {
    public static void main(String[] args) throws IOException {
      // 封装数据源和目的地
      File file1 = new File("d:\\Java");
      File file2 = new File("d:\\jad");

     // 文件过滤器封装目录下String数组对象,匿名对象
     String[] strArr = file1.list(new  FilenameFilter() {

        @Override
        public boolean  accept(File dir, String name) {
        // 判断条件文件和后缀
          return new File(dir, name).isFile() && name. endsWith(".java");
       }
      });

      // 遍历
      for (String str : strArr) {

        // 输入流,封装符合条件的数据源
        BufferedReader br = new BufferedReader(new FileReader(
                                    new File(file1, str)));

        // 输出流,改名并封装要装入的目的地
        BufferedWriter bw = new BufferedWriter(new FileWriter(
        new File(file2, str. replace(".java", ".jad"))));

        // 遍历读写文件
        String line = null;
        while ((line = br.readLine()) != null) {
               bw.write(line);
               bw.newLine();
               bw.flush();
          }
          br.close();
          bw.close();
     }
   }
}

你可能感兴趣的:(java)