国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - 使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實現(xiàn)方法分享

使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實現(xiàn)方法分享

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

這篇文章主要介紹了使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實現(xiàn)方法分享,利用到了Java中的文件io包,需要的朋友可以參考下

先以簡單的文件讀寫實現(xiàn)為基礎(chǔ),F(xiàn)ileHelper類中的readFile方法用于讀取文件內(nèi)容,writeFile方法用于向文件中寫入內(nèi)容。

?
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類的基礎(chǔ)上寫一個WebpageMaker類,其createPage方法用于將特定文件中的內(nèi)容生成在特定的網(wǎng)頁中。
其中如果要插入代碼可以將代碼加入中。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产激情91久久精品导航 | jlzzjlzz亚洲日本少妇 | 秋霞电影院午夜伦 | 天天色天天射天天操 | 岛国av一区 | 亚洲电影在线播放 | 四虎永久免费影院 | 黄色一级片免费观看 | 91精品视频在线播放 | 色综合网在线 | 精品一区二区三区免费毛片爱 | 午夜操操| 国内精品视频 | 69久久 | 97精品国产97久久久久久免费 | 久久成人国产精品 | 亚洲欧美一区二区三区不卡 | 久久精品小视频 | 久久久一区二区三区 | 久久婷婷av | 麻豆av电影在线观看 | 一级在线看| 成人精品一区二区三区 | 日本电影一区 | 欧美激情视频一区二区三区在线播放 | 成人影院在线 | 亚洲欧美视频 | wwwav在线播放 | 国产毛片一区二区 | 国产在线一区二区 | www中文字幕 | 欧美一级特黄视频 | 精品国产青草久久久久福利 | 国产精品福利视频 | 99久久精品一区二区成人 | 黄色大片在线播放 | 亚洲精品一区二区三区樱花 | 精品国产乱码久久久久久1区2区 | 久久精品xx老女人老配少 | 最新中文字幕在线 | a级毛片黄 |