march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          之前寫了一個FileHelper類用于實現(xiàn)文件的讀取和寫入。
          這次在原來的基礎(chǔ)上寫了一個WebpageMaker類,其createPage方法用于將特定文件中的內(nèi)容生成在特定的網(wǎng)頁中。
          其中如果要插入代碼可以將代碼加入中。
          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 += "&quot;";
                          else if(c == '&') ans += "&amp;";
                          else if(c == '<') ans += "&lt;";
                          else if(c == '>') ans += "&gt;";
                          else if(c == ' ') ans += "&nbsp;";
                          else if(c == '\t') ans += "&nbsp;&nbsp;&nbsp;&nbsp;";
                          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;
          }
          posted on 2015-03-06 16:36 marchalex 閱讀(365) 評論(2)  編輯  收藏 所屬分類: java小程序

          FeedBack:
          # re: Java根據(jù)文本內(nèi)容生成網(wǎng)頁
          2015-03-06 23:52 | gaochang
          servlet?  回復(fù)  更多評論
            
          # re: Java根據(jù)文本內(nèi)容生成網(wǎng)頁
          2015-03-07 00:10 | marchalex
          @gaochang
          暫時還不會servlet,以后有時間可能會研究吧:)  回復(fù)  更多評論
            
          主站蜘蛛池模板: 山阴县| 辽阳市| 麦盖提县| 兰西县| 清河县| 吐鲁番市| 石家庄市| 枣庄市| 正安县| 呼和浩特市| 普宁市| 广饶县| 峡江县| 清苑县| 甘泉县| 高密市| 滦南县| 陆丰市| 潞城市| 青铜峡市| 茶陵县| 花莲县| 郑州市| 淳化县| 朝阳市| 当阳市| 改则县| 肃南| 迁西县| 磴口县| 彭泽县| 金湖县| 桓台县| 阳信县| 金门县| 万年县| 法库县| 二连浩特市| 改则县| 南乐县| 周宁县|