使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁(yè)的實(shí)現(xiàn)方法分享
先以簡(jiǎn)單的文件讀寫(xiě)實(shí)現(xiàn)為基礎(chǔ),F(xiàn)ileHelper類(lèi)中的readFile方法用于讀取文件內(nèi)容,writeFile方法用于向文件中寫(xiě)入內(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類(lèi)的基礎(chǔ)上寫(xiě)一個(gè)WebpageMaker類(lèi),其createPage方法用于將特定文件中的內(nèi)容生成在特定的網(wǎng)頁(yè)中。
其中如果要插入代碼可以將代碼加入中。
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ù)庫(kù)密碼加密的方法
由于系統(tǒng)安全的考慮,配置文件中不能出現(xiàn)明文密碼的問(wèn)題,本文就給大家詳細(xì)介紹下springboot配置數(shù)據(jù)庫(kù)密碼加密的方法,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧,需要的朋友可以參考下2023-08-08
SpringBoot注解@EnableScheduling定時(shí)任務(wù)詳細(xì)解析
這篇文章主要介紹了SpringBoot注解@EnableScheduling定時(shí)任務(wù)詳細(xì)解析,@EnableScheduling 開(kāi)啟對(duì)定時(shí)任務(wù)的支持,啟動(dòng)類(lèi)里面使用@EnableScheduling 注解開(kāi)啟功能,自動(dòng)掃描,需要的朋友可以參考下2024-01-01
java使用jdbc連接數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了java使用jdbc連接數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
springboot項(xiàng)目之相互依賴報(bào)錯(cuò)問(wèn)題(基于idea)
這篇文章主要介紹了springboot項(xiàng)目之相互依賴報(bào)錯(cuò)問(wèn)題(基于idea),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
總結(jié)Java常用到的六個(gè)加密技術(shù)和代碼
大家要記住現(xiàn)代密碼學(xué)最重要的原則柯克霍夫原則:數(shù)據(jù)的安全基于密鑰而不是算法的保密。也就是說(shuō)即使密碼系統(tǒng)的任何細(xì)節(jié)已為人悉知,只要密匙未洩漏,它也應(yīng)是安全的。這篇文章給大家介紹了6個(gè)常用的加密技術(shù)和代碼。2016-07-07
Java基礎(chǔ)入門(mén)篇之邏輯控制練習(xí)題與猜數(shù)字游戲
猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說(shuō)簡(jiǎn)單也很簡(jiǎn)單,說(shuō)不簡(jiǎn)單確實(shí)也很難,這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)入門(mén)篇之邏輯控制練習(xí)題與猜數(shù)字游戲的相關(guān)資料,需要的朋友可以參考下2023-06-06
springboot整合redis修改分區(qū)的操作流程
這篇文章主要介紹了springboot整合redis修改分區(qū)的操作流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring通過(guò)構(gòu)造函數(shù)注入實(shí)現(xiàn)方法分析
這篇文章主要介紹了spring通過(guò)構(gòu)造函數(shù)注入實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring通過(guò)構(gòu)造函數(shù)注入的原理、實(shí)現(xiàn)步驟及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-10-10

