private boolean saveFiles(String fileName,String fileUrl) throws Exception {
boolean flag = false;
try {
File srcDir = new File(fileUrl);
if(!(srcDir.exists()&& srcDir.isDirectory()))
throw new Exception("此路径不存在!!");
File[] files= srcDir.listFiles(
new FilenameFilter(){
public boolean accept(File dir, String name) {
boolean flag = false;
for(int i = 0;i < endName.length;i++){
if(name.endsWith(endName[i])){
flag = true;
break;
}
}
return flag;
}
}
);
System.out.println(files.length);
File destDir= new File(fileName);
if(!destDir.exists())
destDir.mkdir();
for(File f:files){
FileInputStream fis = new FileInputStream(f);
String destFileName = f.getName().replaceAll("\\.java$", ".jad");
FileOutputStream fos = new FileOutputStream(new File(destDir,destFileName));
copy(fis,fos);
fis.close();
fos.close();
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
private void copy(FileInputStream ips, FileOutputStream ops) throws IOException {
int len = 0;
byte[] buf = new byte[1024];
while((len = ips.read(buf)) != -1){
ops.write(buf,0,len);
}
}
其中,filename为目标路径,fileUrl为要备份的文件的路径。
endName是备份文件夹文件的后缀名的数组
private static final String[] endName = {".xlsx",".java",".txt",".jpg",".dll"};//文件类型