在开发过程中, 有时候需要将.html文件变成.jsp文件,并添加utf-8字符集,一个一个手动去改往往比较浪费时间,所以写了个工具类
具体代码如下:
package com.jym.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class JymFileUtil {
public static void change2jsp(File file) throws IOException {
File[] files = file.listFiles();
for (File a : files) {
if (a.isDirectory()) {
change2jsp(a);
}
html2jsp(a);
}
}
private static void html2jsp(File file) throws IOException{
String name = file.getName();
if (name.endsWith(".html")) {
File tempFile = new File(file.getAbsolutePath().replace(".html", ".jsp"));
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
FileOutputStream outFile = new FileOutputStream(tempFile);
OutputStreamWriter ow = new OutputStreamWriter(outFile, "UTF-8");
String s = "<%@page pageEncoding=\"UTF-8\" contentType=\"text/html; charset=UTF-8\" %>\r\n";
ow.write(s, 0, s.length());
char[] buffer = new char[1024];
int i = 0;
while ((i = isr.read(buffer)) != -1) {
ow.write(buffer, 0, i);
}
ow.close();
isr.close();
outFile.close();
file.delete();
}
}
}
世界上有10种人,一种是懂二进制的,一种是不懂二进制的。
感谢您的收看,如有哪里写的不对 请留言,谢谢。