java中重命名文件并移动文件

今天遇到一个需求,需要把有1000行的文件重命名为*.done并移动到新的目录当中, 本来很简单的需求,实际也真的很简单, 浪费很多时间太不应该, 代码如下.


private void archiveAuditLogFile(String oldFileAbsolutePath){
String archiveFolderPath = this.auditBasicPath + DateUtils.format(new Date(), "yyyyMM");
File dirFile = null;
try{
dirFile = new File(archiveFolderPath);
if (!(dirFile.exists())&&!(dirFile.isDirectory())) {
boolean creadok = dirFile.mkdirs();
if (creadok) {
System.out.println( "===========create archive folder successfully!===============:"+dirFile.getName());
} else {
System.out.println( "====================err: failed to create archive folder==============" );
}
}else{
System.out.println("==================the folder "+ archiveFolderPath + "alredy exist!=========");
}

File file = new File(oldFileAbsolutePath);
File newFile = new File(archiveFolderPath+ File.separator+file.getName()+".done");
file.renameTo(newFile);
System.out.println("new file Name :"+ newFile.getName()+"\t file path:"+newFile.getParent());

}catch(Exception se){
se.printStackTrace();
}
}


简单而言就是先根据旧文件的绝对路径创建file, 再指定新文件的绝对路径(包含新的目录和文件名即可), 然后remaneTo就搞定了!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值