public class CopyFile2 {
public static void main(String[] args) throws IOException{
File src=new File("c:\\javae\\day10");
File des=new File("c:\\javaee\\naxienian");
copyFile(src,des);
}
public static void copyFile(File src,File des) throws IOException{
if(!src.exists())
throw new RuntimeException();
if(!des.exists()&&(!des.isDirectory()))
des.mkdirs();
File[] files=src.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
return (new File(dir,name).isFile())&&(name.endsWith(".java"));
}
});
for(File f:files){
String name=f.getName().replace(".java",".txt");
File f1=new File(des,name);
BufferedReader br=new BufferedReader(new FileReader(f));
BufferedWriter bw=new BufferedWriter(new FileWriter(f1));
String line=null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
}
|
|