您的位置:首页 > 编程语言 > Java开发

在java中使用 File.renameTo(File)实现重命名.

2013-12-19 03:11 302 查看
Here is part of my files:

public static void main(String[] args) {
String path = "H:\\Java\\JavaSE\\";
File root = new File(path);
String oriName = "";
String newName = "";
String toClip = "[北京圣思园Java培训教学视频]Java.SE.";
for (String f : root.list()) {
File fSub = new File(path + f); //File to be rename(Must append from the root path).
if (!fSub.isFile())    //Only operate File.
continue;
if (fSub.getName().startsWith(toClip)) {
oriName =fSub.getName();
newName = oriName.substring(toClip.length());
File fSubNew = new File(path + newName);    //The target File.
if(fSub.renameTo(fSubNew))
System.out.println(fSub.getAbsolutePath() + " rename successfully");
else
System.out.println(fSub.getAbsolutePath() + " rename failed");
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: