更新時間:2022-09-21 10:20:28 來源:動力節(jié)點 瀏覽3103次
Java生成html文件要怎樣做?代碼是什么?動力節(jié)點小編來告訴大家。
在eclipse中,用java動態(tài)生成html文件。
//用于存儲html字符串
StringBuilder stringHtml = new StringBuilder();
try{
//打開文件
PrintStream printStream = new PrintStream(new FileOutputStream("./Data/test.html"));
}catch(FileNotFoundException e){
e.printStackTrace();
}
//輸入HTML文件內容
stringHtml.append("<html><head>");
stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">");
stringHtml.append("<title>測試報告文檔</title>");
stringHtml.append("</head>");
stringHtml.append("<body>");
stringHtml.append("<div>hello</div>");
stringHtml.append("</body></html>");
try{
//將HTML文件內容寫入文件中
printStream.println(stringHtml.toString());
}catch (Exception e) {
e.printStackTrace();
}
注意:生成html文件的需要需要注意meta頭部的charset的配置。