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

删除图片的就不放上来了,想要实现的可以将后缀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);
		}
	}



猜你喜欢

转载自blog.csdn.net/qq_29048719/article/details/77103503