//编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt。
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
class Demo2
{
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader (new FileReader("c:\\kaifa\\Javacode\\day01"));
//PrintWriter pw = new PrintWriter("d:\\aaaa\\bbb\\aa.txt");
File fil = new File("e:\\aaaa\\bbb");
if(!(fil.exists())){
fil.mkdirs();
}
File files = new File("D:\\day01");
File[] fi = files.listFiles();
for(File file1 : fi){
if(file1.isFile() && file1.toString().endsWith(".java")){
String s= file1.getName().replace(".java",".txt");
BufferedReader br = new BufferedReader (new FileReader(file1));
PrintWriter pw = new PrintWriter("e:\\aaaa\\bbb\\"+s);
//BufferedWriter bw = new BufferedWriter(new FileWriter("e:\\aaaa\\bbb\\"+s));
char[] ch = new char[5];
int len =0;
while((len=br.read(ch))!=-1){
pw.write(ch,0,len);
//bw.flush();
}
br.close();
pw.close();
}
}
}
}
|
|