实现读取文件夹中文件,复制到另外一个文件夹,并且删除原来的文件包括(图片和PDF)

本文介绍了一个Java程序中实现文件复制及目录下所有文件删除的功能。该程序首先检查指定路径是否存在文件,并选择特定文件进行复制操作,然后遍历并删除指定目录下的所有文件。

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

删除图片的就不放上来了,想要实现的可以将后缀pdf改成jpg

public static String monitorPic() {
		String s1 = "C:\\Users\\weidx\\Documents\\My Access-IS Data\\Images";
		File file = new File(s1);
		File[] fileList = file.listFiles();
		j=j+1;
		String s2 = "C:\\Users\\weidx\\Documents\\My Access-IS Data\\copyPic\\"+j+".jpg";
		if (fileList.length > 0) {
			String pth;
			if (fileList.length>4) {
				 pth = fileList[4].getPath();
			}else {
				pth= fileList[3].getPath();
			}
			try {
				s2=copyPic(pth, s2);
			      for (int i = 0; i < fileList.length; i++) {
			      System.out.println("删除文件:"+ i+fileList[i].delete());  
			        }
			} catch (Exception e) {
				e.printStackTrace();
			}
			return s2;
		} else {
			
		}
		return null;
	}
	


复制文件

public static String copyFile(String s1, String s2) throws Exception {
		File f1 = new File(s1);
		File f2 = new File(s2);
		while (f2.exists()) {
			i=i+1;
	   s2 = "C:\\Users\\weidx\\Documents\\My Access-IS Data\\copyPDf\\"+i+".pdf";
	   f2 = new File(s2);
		}
		long time = new Date().getTime();
		int length = 2097152;
		FileInputStream in = new FileInputStream(f1);
		FileOutputStream out = new FileOutputStream(f2);
		byte[] buffer = new byte[length];
		while (true) {
			int ins = in.read(buffer);
			if (ins == -1) {
				in.close();
				out.flush();
				out.close();
				return s2;
			} else
				out.write(buffer, 0, ins);
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值