public boolean renameTo(File dest)
重新命名此抽象路径名表示的文件。
此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从一个文件系统移动到另一个文件系统,dest为新命名的抽象文件
public boolean ReName(String path,String newname) {//文件重命名
//Scanner scanner=new Scanner(System.in);
File file=new File(path);
if(file.exists()) {
File newfile=new File(file.getParent()+File.separator+newname);//创建新名字的抽象文件
if(file.renameTo(newfile)) {
System.out.println("重命名成功!");
return true;
}
else {
System.out.println("重命名失败!新文件名已存在");
return false;
}
}
else {
System.out.println("重命名文件不存在!");
return false;
}
}