1、string创建之后是不可修改的。
String s1 = files[i].getName();
StringBuffer s2 = new StringBuffer(files[i].getName());
2、示例
public class ChangeFileName {
public static void main(String[] args) throws Exception {
File file = new File("movies");
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String s1 = files[i].getName();
StringBuffer s2 = new StringBuffer(files[i].getName());
if (s1.matches("movies[0-9]_[0-9].txt")) {
s2.insert(6, "0");
s2.insert(9, "0");
} else if (s1.matches("movies[0-9]_[0-9]{2}.txt"))
s2.insert(6, "0");
else if (s1.matches("movies[0-9]{2}_[0-9].txt"))
s2.insert(9, "0");
files[i].renameTo(new File("movies\\"+s2));
}
}
}