用main方法传参
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Change {
String textHtml = "<html><head>" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
"</head>\n<body>\n<div>";
String color = "#00688B";
//读取文件
public void ReadFile(String filePath) throws IOException {
File file = new File(filePath);
File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
// 遍历,目录下的所有文件
for (File f : files) {
if (f.isFile()) {
if (!f.getCanonicalPath().contains("-user-")|| !f.getCanonicalPath().contains("ServiceImpl.java")) {
continue;
}
if (!f.getName().endsWith("java")) {
continue;
}
InputStreamReader in=null;
BufferedReader bu=null;
try {
in = new InputStreamReader(new FileInputStream(f));
bu = new BufferedReader(in);
textHtml += "<p>\n";
textHtml += "<br>" + "<b>"+ "-------------" +"</b>"+ "<br>";
String lineText = null;
while ((lineText = bu.readLine()) != null) {
if (lineText.trim().startsWith("*")||lineText.trim().startsWith("/")||lineText.trim().startsWith("import")) {
continue;
}
if (lineText.trim().length()==0) {
continue;
}
lineText = changeToHtml(lineText);
lineText += "<br>\n";
textHtml += lineText;
}
textHtml += "</p>\n";
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bu.close();
} catch (IOException e) {
e.printStackTrace();
}
try
{
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
} else if (f.isDirectory()) {
System.out.println("now enter directory:" + f.getAbsolutePath());
ReadFile(f.getAbsolutePath());
}
}
}
private static final String regex ="[\u4e00-\u9fa5]";
static String getNoChinese(String text) {
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(text);
String repickStr = mat.replaceAll("");
return repickStr;
}
//输出文件
public void writerFile(String writepath) {
textHtml += "</div></html></body>";
String finalText = getNoChinese(textHtml);
File file = new File(writepath);
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(file));
output.write(finalText);
} 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("\t", " ");
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>");
return text;
}
public static void main(String[] args) throws IOException {
Change c = new Change();
c.ReadFile("C:\\Users\\caishuxiao\\git-pullOnly\\ShiShangDiYiGeKeYongBanBen\\trunk\\sh-yfkj");
c.writerFile("C:\\reconfig\\gorupiah_reconfiguration\\sh-yfkj\\common\\src\\main\\java\\com\\yf\\common\\utils\\nb.html");
}
}