java 复制文件夹中epub、html、txt文件 (按原来文件夹存放)

原来文件夹中的文件:有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文件







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值