吧一个文件夹下的java源代码转成html

用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 = "" + 
        "" + 
        "\n\n
"; 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 += "

\n"; textHtml += "
"
+ ""+ "-------------" +""+ "
"
; 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 += "
\n"
; textHtml += lineText; } textHtml += "

\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 += "
"
; 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", "public"); text = text.replace("class", "class"); text = text.replace("static", "static"); text = text.replace("void", "void"); 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"); } }

你可能感兴趣的:(java)