原来文件夹中的文件:有epub/html/txt
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 复制文件夹中所有包含.epub后缀的文件
* @author fibre
* parameter SUFFIX = ".epub"
*/
public class CopyFileFolder {
private static String SUFFIX = ".epub";
public void copyFolder(String folder, String newPath) throws IOException{
File old = new File(folder);
File[] fileArray = old.listFiles();
for(File file: fileArray){
if(file.isFile()){
if(file.getName().endsWith(SUFFIX)){
//判断是否存在目的文件夹
File newPathFile = new File(newPath);
if(!newPathFile.isDirectory()){
newPathFile.mkdirs();
}
//开始复制
try {
FileInputStream ins = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(newPath+"/"+file.getName());
System.out.println("!!文件复制:"+file.getAbsolutePath()+"----->"+newPath+"/"+file.getName());
byte[] b = new byte[1024 * 5];
int len;
while( (len=ins.read(b)) != -1){
out.write(b);
}
ins.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(file.isDirectory()){
copyFolder(file.getAbsolutePath(),newPath+"/"+file.getName());
System.out.println("【文件夹复制】:"+file.getAbsolutePath()+"----->"+newPath+"/"+file.getName());
}
}
}
public static void main(String[] arg) throws IOException{
//可以改成复制后缀为html的文件
//CopyFileFolder.SUFFIX = ".html";
String folder = "F://Resource/知乎/epub/知乎各专业回答集锦";
String target = "F://Resource/知乎/epub/知乎各专业回答集锦(epub)";
CopyFileFolder copy = new CopyFileFolder();
copy.copyFolder(folder,target);
}
}
执行之后:按文件夹存放,只有epub文件