使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁(yè)的實(shí)現(xiàn)方法分享
先以簡(jiǎn)單的文件讀寫實(shí)現(xiàn)為基礎(chǔ),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類的基礎(chǔ)上寫一個(gè)WebpageMaker類,其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)文章
java實(shí)現(xiàn)Dijkstra最短路徑算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Dijkstra最短路徑算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用流程詳細(xì)介紹
OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成2023-02-02
SpringBoot整合定時(shí)任務(wù)之實(shí)現(xiàn)Scheduled注解的過(guò)程(一個(gè)注解全解決)
這篇文章主要介紹了SpringBoot整合定時(shí)任務(wù)之實(shí)現(xiàn)Scheduled注解的過(guò)程(一個(gè)注解全解決),本文通過(guò)使用場(chǎng)景分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào)問(wèn)題
最近遇到一個(gè)問(wèn)題,比如GET請(qǐng)求中,key,value中帶有特殊符號(hào),請(qǐng)求會(huì)報(bào)錯(cuò)。接下來(lái)通過(guò)本文給大家分享解決SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào) ,需要的朋友可以參考下2019-05-05
Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
這篇文章主要介紹了springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java實(shí)現(xiàn)讀取Jar文件屬性的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)讀取Jar文件屬性的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08

