package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Test7 {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//源目录
File dir = new File(str);
//目标目录
File dest = new File("c:\\hello");
read(dir,dest);
}
public static void read(File dir,File dest) throws IOException{
//判断目标目录是否存在,不存在则创建
if(!dest.exists())
dest.mkdir();
//判断目标目录是否是目录,不是则创建
if(!dest.isDirectory())
dest.mkdir();
File[] files = dir.listFiles();
for(File file : files){
if(file.isDirectory())
//如果此File对象是目录,递归进入目录
read(file,dest);
else{
//判断是否是txt类型文件
if(file.getName().endsWith(".txt")){
BufferedReader br = new BufferedReader(new FileReader(file));
//将txt文件复制到目标目录, 并更改扩展名
PrintWriter pw = new PrintWriter(new FileWriter(dest.getAbsolutePath() +File.separator + file.getName().replace(".txt", ".java")));
String line = null;
while((line=br.readLine()) != null){
pw.println(line);
}
br.close();
pw.close();
}
}
}
}
} |