修改文件名
程序员文章站
2022-06-14 16:50:49
...
import java.io.File;
public class Test {
public static void main(String args[]) {
File f = new File("/Users/houyongkang/Downloads/原名.txt");
if (f.exists()) {
String oldname = f.getName();
String newname = newname = "修改后名称.txt";
if (!oldname.equals(newname)) {
String path = f.getParent();
File newfile = new File(path + File.separator + newname);
if (newfile.exists()) {
System.out.println(newname + "已经存在!");
} else {
f.renameTo(newfile);
}
}
} else {
System.out.println("File does not exist!");
}
}
}
下一篇: 怎样鉴别紫米是否染色,教你几招