- package com.itheima;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- /**
- *第9题:编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
- *
- */
- public class Test9 {
- public static void main(String[] args) throws IOException {
- // TODO 自动生成的方法存根
- File old = new File("D:\\java0217\\day03");
-
- File now = new File("E:\\javafile");
-
- if(!now.exists())
-
- now.mkdir();
-
- File[] files = old.listFiles(new FileFilter(){
- public boolean accept(File pathname) {
- // TODO 自动生成的方法存根
- return pathname.getName().endsWith(".java");
- }});
- for(File f : files){
-
- BufferedReader bufr = new BufferedReader(new FileReader(f));
-
- BufferedWriter bufw = new BufferedWriter(new FileWriter(now));
-
- String line = null;
-
- f.getName().replaceAll(".java", ".txt");
-
- while((line = bufr.readLine())!=null){
-
- bufw.write(line);
-
- bufw.newLine();
-
- bufw.flush();
- }
- bufr.close();
-
- bufw.close();
- }
- }
- }
复制代码
|
|