java如何重命名文件?


/**
* 修改文件名
* @param oldFilePath 原文件路径
* @param newFileName 新文件名称
* @param overriding 判断标志(如果存在相同名的文件是否覆盖)
* @return
*/
public static boolean renameFile(String oldFilePath,String newFileName,boolean overriding){
File oldfile = new File(oldFilePath);
if(!oldfile.exists()){
return false;
}
String newFilepath = oldfile.getParent()+File.separator+newFileName;
File newFile = new File(newFilepath);
if(!newFile.exists()){
return oldfile.renameTo(newFile);
}else{
if(overriding){
newFile.delete();
return oldfile.renameTo(newFile);
}else{
return false;
}
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM