一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 使用Java把文本內容轉換成網頁的實現方法分享

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

2020-01-21 14:56march alex JAVA教程

這篇文章主要介紹了使用Java把文本內容轉換成網頁的實現方法分享,利用到了Java中的文件io包,需要的朋友可以參考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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方法用于將特定文件中的內容生成在特定的網頁中。
其中如果要插入代碼可以將代碼加入中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

?
1
2
3
4
5
6
7
hello world!
大家好:)
#include
int main() {
  printf("hello world!\n");
  return 0;
}

輸出文件:test.html

?
1
2
3
4
5
6
7
8
9
<!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>

效果如下:

?
1
2
3
4
5
6
7
hello world!
大家好:)
#include <stdio.h>
int main() {
  printf("hello world!\n");
  return 0;
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 师尊被各种play打屁股 | 久久精品国产亚洲AV热无遮挡 | 国色天香社区视频在线观看免费完整版 | 草莓香蕉绿巨人丝瓜榴莲18 | 五月激情丁香婷婷综合第九 | a及毛片 | 色欧美亚洲 | 99久久精品免费看国产一区二区 | 国产成人综合视频 | 国产一卡二卡3卡4卡四卡在线视频 | 亚洲国产精品网 | 精品欧美一区二区三区在线观看 | 国内精品伊人久久大香线焦 | 99国内精品| 亚洲国产综合网 | 无限资源在线观看播放 | 精品成人网 | 双性太子| 国产aaaaa一级毛片 | 免费精品国产 | 亚洲另类激情 | 欧美怡红院视频一区二区三区 | 天天干天天操天天碰 | 精品视频 久久久 | 日韩一级在线观看 | 精品国产精品国产偷麻豆 | 免费一区二区 | 色婷婷久久综合中文久久一本 | 日韩高清一区二区三区不卡 | 亚洲乱码一区二区三区国产精品 | 西野翔全部作品在线观看 | narutomanga玖辛奈本子 | 成人在线一区二区三区 | 亚欧有色在线观看免费版高清 | 日本丰满www色 | 亚洲精品视频在线 | 免费看a视频 | 天天操天天射天天爽 | 美日韩在线观看 | 国产一区二区三区久久精品 | 成在线人免费 |