java中将java文件转换为html一个文件,先使用file类读取java文件,然后使用string进行分割、替换等 *** 作,输出html后缀名的文件,如下代码:
import java.io.BufferedReaderimport java.io.BufferedWriter
import java.io.File
import java.io.FileInputStream
import java.io.FileWriter
import java.io.IOException
import java.io.InputStreamReader
public class Change {
String textHtml = ""
String color = "#00688B"
//读取文件
public void ReadFile(String filePath) {
BufferedReader bu = null
InputStreamReader in = null
try {
File file = new File(filePath)
if (file.isFile() && file.exists()) {
in = new InputStreamReader(new FileInputStream(file))
bu = new BufferedReader(in)
String lineText = null
textHtml = "<html><body>"
while ((lineText = bu.readLine()) != null) {
lineText = changeToHtml(lineText)
lineText += "</br>"
textHtml += lineText
}
textHtml += "</html></body>"
} else {
System.out.println("文件不存在")
}
} catch (Exception e) {
e.printStackTrace()
} finally {
try {
bu.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
//输出文件
public void writerFile(String writepath) {
File file = new File(writepath)
BufferedWriter output = null
try {
output = new BufferedWriter(new FileWriter(file))
System.out.println(textHtml)
output.write(textHtml)
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
output.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
//文件转换
public String changeToHtml(String text) {
text = text.replace("&", "&")
text = text.replace(" ", " ")
text = text.replace("<", "<")
text = text.replace(">", ">")
text = text.replace("\"", """)
text = text.replace(" ", " ")
text = text.replace("public", "<b><font color='"+color+"'>public</font></b>")
text = text.replace("class", "<b><font color='"+color+"'>class</font></b>")
text = text.replace("static", "<b><font color='"+color+"'>static</font></b>")
text = text.replace("void", "<b><font color='"+color+"'>void</font></b>")
String t = text.replace("//", "<font color=green>//")
if (!text.equals(t)) {
System.out.println("t:"+t)
text = t + "</font>"
}
return text
}
public static void main(String[] args) {
System.out.println("第一个参数为读取文件路径,第二个参数为生成文件路径")
if(args.length<1){
System.out.println("请<a href="https://www.baidu.com/s?wd=%E8%BE%93%E5%85%A5%E6%96%87%E4%BB%B6&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3P16znjKBn1uWPvnzPWcY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DsPjTdnWTvPjT3n1T4n1ckn1b" target="_blank" class="baidu-highlight">输入文件</a>路径")
return
}else if(args.length<2){
System.out.println("请输入生成文件")
return
}
Change c = new Change()
c.ReadFile(args[0])
c.writerFile(args[1])
}
}
用正则,我找个自己用的代码给你。private static String rep="<[\\s]*?script[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?script[\\s]*?>" +
"|<[\\s]*?div[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?div[\\s]*?>"+
"|<[\\s]*?style[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?style[\\s]*?>"+
"|<[\\s]*?form[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?forum[\\s]*?>"+
"|<[\\s]*?input[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?select[\\s]*?>"+
"|<[\\s]*?<marquee [^>]*?>"+
"|<[\\s]*?\\/[\\s]*?<marquee [\\s]*?>" +
"|<[\\s]*?ul[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?ul[\\s]*?>" +
"|<[\\s]*?li[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?li[\\s]*?>" +
"|<[\\s]*?div[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?div[\\s]*?>" +
"|<[\\s]*?table[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?table[\\s]*?>" +
"|<[\\s]*?tr[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?tr[\\s]*?>" +
"|<[\\s]*?td[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?td[\\s]*?>" +
"|<[\\s]*?th[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?th[\\s]*?>"
/**
* 去掉危险的代码[div][style][form][input][script][ul][li][table][tr][td][th]
* @param content
* @return
*/
public static String removeDangerTag(String content){
Pattern p = null
Matcher m = null
p = Pattern.compile(rep,Pattern.CASE_INSENSITIVE)//去掉危险的HTML
m = p.matcher(content)
String temp = m.replaceAll("")
return temp
}
//-----------按照你的要求,吧相应的p加入到正则匹配中
private static String rep="<[\\s]*?script[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?script[\\s]*?>" +
"|<[\\s]*?div[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?div[\\s]*?>"+
"|<[\\s]*?style[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?style[\\s]*?>"+
"|<[\\s]*?form[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?forum[\\s]*?>"+
"|<[\\s]*?input[^>]*?>"+
"|<[\\s]*?\\/[\\s]*?select[\\s]*?>"+
"|<[\\s]*?<marquee [^>]*?>"+
"|<[\\s]*?\\/[\\s]*?<marquee [\\s]*?>" +
"|<[\\s]*?ul[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?ul[\\s]*?>" +
"|<[\\s]*?li[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?li[\\s]*?>" +
"|<[\\s]*?div[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?div[\\s]*?>" +
"|<[\\s]*?table[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?table[\\s]*?>" +
"|<[\\s]*?tr[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?tr[\\s]*?>" +
"|<[\\s]*?td[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?td[\\s]*?>" +
"|<[\\s]*?p[^>]*?>" + //这里加入了p标签
"|<[\\s]*?\\/[\\s]*?p[\\s]*?>" +
"|<[\\s]*?th[^>]*?>" +
"|<[\\s]*?\\/[\\s]*?th[\\s]*?>"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)