java檔案重命名,并且保留老的檔案,實際上就是拷貝一份新的檔案,相當于複制粘貼重命名。代碼如下:
傳參數說明:老的檔案位址,oneType和twoType還有count是我自己業務的東西也是檔案的重命名名字,files集合是為了友善我把這批檔案導出且壓縮,參見 https://blog.csdn.net/erpenggg/article/details/106937129
//對圖檔進行重命名
public String reNameImg(String oldPath, String oneType, String twoType, int count, List<File> files) {
File source = new File(oldPath);
String suffixName = source.getName().substring(source.getName().lastIndexOf("."));
File filePath = new File(rootFileDir + File.separator + "exPort" + File.separator +generatorDataFileName());
String reName = "";
try {
if (!filePath.exists()) {
filePath.mkdirs();
}
File dest =new File(filePath+File.separator+oneType + "-" + twoType + "-" + count + suffixName);
Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
files.add(dest);
reName = dest.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return reName;
}
//擷取日期目錄
private String generatorDataFileName() {
Calendar date = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(date.getTime());
}