首页 > 开发 > Java > 正文

使用Java把文本内容转换成网页的实现方法分享

2024-07-13 09:56:06
字体:
来源:转载
供稿:网友

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

先以简单的文件读写实现为基础,FileHelper类中的readFile方法用于读取文件内容,writeFile方法用于向文件中写入内容。

 

 
  1. import java.io.BufferedReader; 
  2. import java.io.BufferedWriter; 
  3. import java.io.FileReader; 
  4. import java.io.FileWriter; 
  5.  
  6.  
  7.  
  8. public class FileHelper { 
  9. public static String readFile(String filename) throws Exception { 
  10. BufferedReader reader = new BufferedReader(new FileReader(filename));  
  11. String ans = "", line = null
  12. while((line = reader.readLine()) != null){ 
  13. ans += line + "/r/n"
  14. reader.close(); 
  15. return ans; 
  16. public static void writeFile(String content, String filename) throws Exception { 
  17. BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); 
  18. writer.write(content); 
  19. writer.flush(); 
  20. writer.close(); 
  21. public static void main(String[] args) throws Exception { 
  22. String ans = readFile("D://input.txt"); 
  23. writeFile(ans, "D://output.txt"); 

然后在FileHelper类的基础上写一个WebpageMaker类,其createPage方法用于将特定文件中的内容生成在特定的网页中。

其中如果要插入代码可以将代码加入中。

 

 
  1. import java.util.StringTokenizer; 
  2.  
  3.  
  4. public class WebpageMaker { 
  5. public static String initBegin() { 
  6. String s = "<!doctype html><html><head><title></title></head><body>/r/n"
  7. return s; 
  8. public static String initEnd() { 
  9. String s = "/r/n</body></html>/r/n"
  10. return s; 
  11. public static void createPage(String inputfilename, String outputfilename) throws Exception { 
  12. String content = FileHelper.readFile(inputfilename); 
  13. StringTokenizer st = new StringTokenizer(content, "/r/n"); 
  14. String ans = ""
  15. ans += initBegin(); 
  16. boolean isCoding = false
  17. while(st.hasMoreElements()) { 
  18. String s = st.nextToken(); 
  19. int len = s.length(); 
  20. for(int i=0;i<len;i++) { 
  21. if(i+6 <= len && s.substring(i,i+6).equals("<alex>")) { 
  22. isCoding = true
  23. ans += "<pre style=/"background-color:aliceblue/">"
  24. i += 5; 
  25. continue
  26. if(i+7 <= len && s.substring(i,i+7).equals("</alex>")) { 
  27. isCoding = false
  28. ans += "</pre>"
  29. i += 6; 
  30. continue
  31. char c = s.charAt(i); 
  32. if(c == '/"') ans += """
  33. else if(c == '&') ans += "&"
  34. else if(c == '<') ans += "<"
  35. else if(c == '>') ans += ">"
  36. else if(c == ' ') ans += ""
  37. else if(c == '/t') ans += ""
  38. else ans += c; 
  39. if(false == isCoding) 
  40. ans += "<br />/r/n"
  41. else 
  42. ans += "/r/n"
  43. ans += initEnd(); 
  44. FileHelper.writeFile(ans, outputfilename); 
  45. public static void main(String[] args) throws Exception { 
  46. createPage("D://test.txt""D://test.html"); 

样例:

输入文件:test.txt

 

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

输出文件:test.html

 

 
  1. <!doctype html><html><head><title></title></head><body> 
  2. hello world!<br /> 
  3. 大家好:)<br /> 
  4. <pre style="background-color:aliceblue">#include <stdio.h> 
  5. int main() { 
  6. printf("hello world!/n"); 
  7. return 0; 
  8. }</pre><br /> 
  9. </body></html> 

效果如下:

 

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


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表