在学习了JavaEE,要把页面原型后缀名“.html”修改为“.jsp”。我是一个不喜欢做重复动作的人。就想着用代码实现。Java学的还不是很熟练,折腾了半个多钟。还好算能用了。运行那一下特爽。
代码贴下面了。第一次发博客,大家如果有更好的代码实现或者建议也请指导一下。谢谢啦
import java.io.File;
public class ReName {
public static void main(String[] args) {
//要改的文件夹路径
String path= "E:/传智播客_就业班/day24_项目实战之分组演练01/页面";
getNew(path);
}
private static void getNew(String path) {
File file = new File(path);
//得到文件夹下的所有文件和文件夹
String[] list = file.list();
if(list!=null && list.length>0){
for (String oldName : list) {
File oldFile = new File(path,oldName);
//判断出文件和文件夹
if(!oldFile.isDirectory()){
//文件则判断是不是要修改的
if(oldName.contains(".html")){
System.out.println(oldName);
String newoldName = oldName.substring(0, oldName.lastIndexOf("."))+".jsp";
System.out.println(newoldName);
File newFile = new File(path,newoldName);
boolean flag = oldFile.renameTo(newFile);
System.out.println(flag);
}
}else{
//文件夹则迭代
String newpath=path+"/"+oldName;
getNew(newpath);
}
}
}
}
}