本帖最后由 彭卫红 于 2014-8-30 23:38 编辑
- 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();
- }
- }
- }
复制代码
|