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());
}