日期:2014-05-19 浏览次数:20770 次
package com.test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; char[] ch = new char[1024]; int n = 0; List<File> files = getFiles("d:\\java"); for(File file : files){ try { File f = new File("D:\\java1\\" + file.getName()); fr = new FileReader(file); fw = new FileWriter(f); while ((n = fr.read(ch)) != -1) fw.write(ch, 0, n); fw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } String fileName = file.getAbsolutePath(); File fl = new File(fileName.substring(0, fileName.indexOf(".")) + ".jad"); file.renameTo(fl); } } private static List<File> getFiles(String path) { List<File> files = new ArrayList<File>(); File file = new File(path); getAllFiles(file, files); return files; } private static void getAllFiles(File file, List<File> files) { if (file.isFile()) { files.add(file); } else { File[] fs = file.listFiles(); for (File f : fs) { getAllFiles(f, files); } } } }