Java代码修改文件后缀名

在学习了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);
				}
			}
		}
	}
}


  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值