使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實現(xiàn)方法分享
先以簡單的文件讀寫實現(xiàn)為基礎,F(xiàn)ileHelper類中的readFile方法用于讀取文件內(nèi)容,writeFile方法用于向文件中寫入內(nèi)容。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class FileHelper { public static String readFile(String filename) throws Exception { BufferedReader reader = new BufferedReader(new FileReader(filename)); String ans = "", line = null; while((line = reader.readLine()) != null){ ans += line + "\r\n"; } reader.close(); return ans; } public static void writeFile(String content, String filename) throws Exception { BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); writer.write(content); writer.flush(); writer.close(); } public static void main(String[] args) throws Exception { String ans = readFile("D:\\input.txt"); writeFile(ans, "D:\\output.txt"); } }
然后在FileHelper類的基礎上寫一個WebpageMaker類,其createPage方法用于將特定文件中的內(nèi)容生成在特定的網(wǎng)頁中。
其中如果要插入代碼可以將代碼加入中。
import java.util.StringTokenizer; public class WebpageMaker { public static String initBegin() { String s = "<!doctype html><html><head><title></title></head><body>\r\n"; return s; } public static String initEnd() { String s = "\r\n</body></html>\r\n"; return s; } public static void createPage(String inputfilename, String outputfilename) throws Exception { String content = FileHelper.readFile(inputfilename); StringTokenizer st = new StringTokenizer(content, "\r\n"); String ans = ""; ans += initBegin(); boolean isCoding = false; while(st.hasMoreElements()) { String s = st.nextToken(); int len = s.length(); for(int i=0;i<len;i++) { if(i+6 <= len && s.substring(i,i+6).equals("<alex>")) { isCoding = true; ans += "<pre style=\"background-color:aliceblue\">"; i += 5; continue; } if(i+7 <= len && s.substring(i,i+7).equals("</alex>")) { isCoding = false; ans += "</pre>"; i += 6; continue; } char c = s.charAt(i); if(c == '\"') ans += """; else if(c == '&') ans += "&"; else if(c == '<') ans += "<"; else if(c == '>') ans += ">"; else if(c == ' ') ans += " "; else if(c == '\t') ans += " "; else ans += c; } if(false == isCoding) ans += "<br />\r\n"; else ans += "\r\n"; } ans += initEnd(); FileHelper.writeFile(ans, outputfilename); } public static void main(String[] args) throws Exception { createPage("D://test.txt", "D://test.html"); } }
樣例:
輸入文件:test.txt
hello world! 大家好:) #include int main() { printf("hello world!\n"); return 0; }
輸出文件:test.html
<!doctype html><html><head><title></title></head><body> hello world!<br /> 大家好:)<br /> <pre style="background-color:aliceblue">#include <stdio.h> int main() { printf("hello world!\n"); return 0; }</pre><br /> </body></html>
效果如下:
hello world! 大家好:) #include <stdio.h> int main() { printf("hello world!\n"); return 0; }
相關(guān)文章
SpringBoot配置數(shù)據(jù)庫密碼加密的方法
由于系統(tǒng)安全的考慮,配置文件中不能出現(xiàn)明文密碼的問題,本文就給大家詳細介紹下springboot配置數(shù)據(jù)庫密碼加密的方法,下面話不多說了,來一起看看詳細的介紹吧,需要的朋友可以參考下2023-08-08SpringBoot注解@EnableScheduling定時任務詳細解析
這篇文章主要介紹了SpringBoot注解@EnableScheduling定時任務詳細解析,@EnableScheduling 開啟對定時任務的支持,啟動類里面使用@EnableScheduling 注解開啟功能,自動掃描,需要的朋友可以參考下2024-01-01springboot整合redis修改分區(qū)的操作流程
這篇文章主要介紹了springboot整合redis修改分區(qū)的操作流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07spring通過構(gòu)造函數(shù)注入實現(xiàn)方法分析
這篇文章主要介紹了spring通過構(gòu)造函數(shù)注入實現(xiàn)方法,結(jié)合實例形式分析了spring通過構(gòu)造函數(shù)注入的原理、實現(xiàn)步驟及相關(guān)操作注意事項,需要的朋友可以參考下2019-10-10