File srcFolder=new File("c:\demo");
File destFolder=new File("d:\test");
if(!destFolder.exists()){
destFolder.mkdir();
}
File[] fileArray=srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir,name).isFile() && name.endsWith(".java");
}
});
for(File file:fileArray){
System.out.println(file);
String name=file.getName();
File newFile=new File(destFolder,name);
copyFile(file,newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}