a- package test;
- 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 CopyAndRename2 {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- File srcFile = new File("C:\\Users\\hyning\\Desktop\\day22_IO");
- File desFile = new File("C:\\Users\\hyning\\Desktop\\day22_IO\\copyFolder");
- if(!desFile.exists())
- desFile.mkdirs();
- File[] fs = srcFile.listFiles(new FilenameFilter() {
-
- @Override
- public boolean accept(File dir, String name) {
- // TODO Auto-generated method stub
- return new File(dir,name).isFile() && name.endsWith(".java");
- }
- });
- for(File f:fs){
- String outName = f.getName().replaceAll(".java", ".jad");
- File des = new File(desFile,outName);
- copyFile(f,des);
- }
- System.out.println("cope done!!");
- }
- private static void copyFile(File f, File des) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedReader br = new BufferedReader(new FileReader(f));
- BufferedWriter bw = new BufferedWriter(new FileWriter(des));
- String line = null;
- while((line = br.readLine())!=null){
- bw.write(line);
- bw.newLine();
- bw.flush();
- }
- // 释放资源
- br.close();
- bw.close();
- }
- }
复制代码
|
|