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

该博客介绍了一个Java程序,用于复制指定文件夹中所有以.epub为后缀的文件到新的目标路径。程序首先检查目标文件夹是否存在,如果不存在则创建,然后遍历源文件夹,复制每个.epub文件,并打印复制过程信息。同时,程序还具备递归复制子文件夹中.epub文件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原来文件夹中的文件:有epub/html/txt

1304218907304824832.htm

86e58450e7e84dda9d8503dff686b01e.png

04a1228bf5acd6ef282d50ae3ad9e9e9.png

1304218907304824832.htm

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文件

1304218907304824832.htm

1304218907304824832.htm

8fd53adb9360b7438bf3a6a5633187a1.png

f541147f501ca2f5ac9f591a6ea200dc.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值