java批量更改html到jsp

问题描述:将指定目录下的批量的.html静态页面转换成.jsp文件。

思路描述:.html装换成.jsp需要加上两行文件(可以根据需要自行添加),本文中将加入以下两行头文件,并将其保存到head.txt

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class HtmlTransJspUtils {
	public static void main(String[] args) throws Exception {
		//
		File f = new File("指定的目录文件夹");
		for(File s : f.listFiles()){
			String str = s.getAbsolutePath();
			if(str.endsWith(".html")){
				writeFile(str);
				fileRename(str);
			}
			
		}
	}

	/**
	 * 读取指定文件下的内容,乱码问题已经处理,编码为UTF-8
	 * 
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String readFile(String fileName) throws Exception {
		String result = "";
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;
		try {
			inputStreamReader = new InputStreamReader(new FileInputStream(
					fileName), "utf-8");
			bufferedReader = new BufferedReader(inputStreamReader);
			try {
				String read = null;
				while ((read = bufferedReader.readLine()) != null) {
					result = result + read + "\r\n";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bufferedReader != null) {
				bufferedReader.close();
			}
			if (inputStreamReader != null) {
				inputStreamReader.close();
			}
		}
		System.out.println("读取出来的文件内容是:" + "\r\n" + result);
		return result;

	}

	/**
	 * 文件的写入
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	public static void writeFile(String fileName) throws Exception {
		try {
			String oldContent = readFile(fileName);
			FileOutputStream fo = new FileOutputStream(fileName);
			//读取头文件的内容
			fo.write(readFile("F:/head.txt").getBytes("utf-8"));
			fo.write(oldContent.getBytes("utf-8"));
			fo.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改文件的后缀名,即html变成jsp
	 * @param fileName
	 */
	public static void fileRename(String fileName) {
		File oldFile = new File(fileName);
		System.out.println("修改前文件名称是:" + oldFile.getName());
		String rootPath = oldFile.getParent();
		System.out.println("根路径是:" + rootPath);
		File newFile = new File(rootPath , oldFile.getName().replace("html", "jsp"));
		System.out.println("修改后文件名称是:" + newFile.getName());
		if (oldFile.renameTo(newFile)) {
			System.out.println("修改成功!");
		} else {
			System.out.println("修改失败");
		}
	}

}
快乐学习,快乐编程!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值