您的位置:首页 > 其它

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

2017-08-11 21:35 826 查看

删除图片的就不放上来了,想要实现的可以将后缀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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐