国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

使用Java把文本內容轉換成網頁的實現方法分享

2019-11-26 14:50:45
字體:
來源:轉載
供稿:網友

先以簡單的文件讀寫實現為基礎,FileHelper類中的readFile方法用于讀取文件內容,writeFile方法用于向文件中寫入內容。

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方法用于將特定文件中的內容生成在特定的網頁中。
其中如果要插入代碼可以將代碼加入中。

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;}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 介休市| 濉溪县| 葫芦岛市| 乌恰县| 项城市| 连州市| 济宁市| 开化县| 林周县| 和顺县| 河曲县| 柳林县| 会东县| 西乡县| 博湖县| 木里| 板桥市| 浏阳市| 驻马店市| 故城县| 松潘县| 望都县| 鹤峰县| 荃湾区| 柳林县| 新沂市| 连平县| 罗江县| 南部县| 乡城县| 乐东| 高淳县| 宁陕县| 福清市| 崇信县| 建宁县| 临邑县| 鹤庆县| 图木舒克市| 南木林县| 文化|