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

来源:互联网 发布:java nio与io的区别 编辑:程序博客网 时间:2024/05/29 06:53

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



阅读全文
0 0
原创粉丝点击