java复制文件到指定文件夹下,java:把一个文件夹中的所有文件复制到指定文件夹下...

该博客展示了如何使用Java实现将一个文件夹中的所有文件批量复制到另一个文件夹的功能。主要包含两个方法,一个用于复制整个文件夹,另一个用于复制单个文件。在主方法中调用这两个函数完成文件的移动操作。代码中包含了异常处理以确保文件复制的稳定性和安全性。
摘要由CSDN通过智能技术生成

接受tmp1文件夹中的所有文件到tmp2文件夹中

一个主方法和两个函数,其中一个函数要调用另外一个函数。

主方法运行

public static void main(String[] args) {

String path2 = "C://Users//36186//Downloads//CPS-OCR-Engine-master//ocr//tmp2";

String path1 = "C://tessrect//tmp1";

copyFolder(path1, path2);

}

//复制指定文件夹下的所有文件到另一个文件夹下

public static void copyFolder(String strPatientImageOldPath, String strPatientImageNewPath) {

File fOldFolder = new File(strPatientImageOldPath);//旧文件夹

try {

File fNewFolder = new File(strPatientImageNewPath);//新文件夹

if (!fNewFolder.exists()) {

fNewFolder.mkdirs();//不存在就创建一个文件夹

}

File[] arrFiles = fOldFolder.listFiles();//获取旧文件夹里面所有的文件

for (int i = 0; i < arrFiles.length; i++) {

//从原来的路径拷贝到现在的路径,拷贝一个文件

if (!arrFiles[i].isDirectory()) {

copyFile(strPatientImageOldPath + "/" + arrFiles[i].getName(), strPatientImageNewPath + "/" + arrFiles[i].getName());

}

}

} catch (Exception e) {

// TODO: handle exception

}

}

//复制指定文件夹下单个文件到另一个文件夹下

public static void copyFile(String strOldpath,String strNewPath)

{

try

{

File fOldFile = new File(strOldpath);

if (fOldFile.exists())

{

int bytesum = 0;

int byteread = 0;

InputStream inputStream = new FileInputStream(fOldFile);

FileOutputStream fileOutputStream = new FileOutputStream(strNewPath);

byte[] buffer = new byte[1444];

while ( (byteread = inputStream.read(buffer)) != -1)

{

bytesum += byteread; //这一行是记录文件大小的,可以删去

fileOutputStream.write(buffer, 0, byteread);//三个参数,第一个参数是写的内容,

//第二个参数是从什么地方开始写,第三个参数是需要写的大小

}

inputStream.close();

fileOutputStream.close();

}

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

System.out.println("复制单个文件出错");

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值