发布于 2016-07-23 23:16:47 | 127 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java设计模式,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了使用Java把文本内容转换成网页的实现方法分享,利用到了Java中的文件io包,需要的朋友可以参考下

先以简单的文件读写实现为基础,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;
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务