Java实验九第三题
Posted 考研自习室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实验九第三题相关的知识,希望对你有一定的参考价值。
答案我也是嫖的!
https://blog.csdn.net/qq_37246345/article/details/103265037
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 9 /** 10 * 突出显示语法 11 * @author AngoLi 12 */ 13 public class Change { 14 String texthtml = ""; 15 String color = "red"; 16 17 // 读取文件 18 public void ReadFile(String filePath) { 19 BufferedReader bu = null; 20 InputStreamReader in = null; 21 try { 22 File file = new File(filePath); 23 if (file.isFile() && file.exists()) { 24 in = new InputStreamReader(new FileInputStream(file)); 25 bu = new BufferedReader(in); 26 String lineText = null; 27 textHtml = "<html><body>"; 28 while ((lineText = bu.readLine()) != null) { 29 lineText = changeToHtml(lineText); 30 lineText += "</br>"; 31 textHtml += lineText; 32 } 33 textHtml += "</html></body>"; 34 } else { 35 System.out.println("文件不存在"); 36 } 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } finally { 40 try { 41 bu.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 } 47 48 //输出文件 49 public void writerFile(String writepath) { 50 File file = new File(writepath); 51 BufferedWriter output = null; 52 try { 53 output = new BufferedWriter(new FileWriter(file)); 54 System.out.println(textHtml); 55 output.write(textHtml); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } finally { 59 try { 60 output.close(); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 } 65 } 66 67 //文件转换 68 public String changeToHtml(String text) { 69 text = text.replace("&", "&"); 70 text = text.replace(" ", " "); 71 text = text.replace("<", "<"); 72 text = text.replace(">", ">"); 73 text = text.replace(""", """); 74 text = text.replace(" ", " "); 75 text = text.replace("public", "<b><font color=‘"+color+"‘>public</font></b>"); 76 text = text.replace("class", "<b><font color=‘"+color+"‘>class</font></b>"); 77 text = text.replace("static", "<b><font color=‘"+color+"‘>static</font></b>"); 78 text = text.replace("void", "<b><font color=‘"+color+"‘>void</font></b>"); 79 String t = text.replace("//", "<font color=green>//"); 80 if (!text.equals(t)) { 81 System.out.println("t:"+t); 82 text = t + "</font>"; 83 } 84 return text; 85 } 86 87 public static void main(String[] args) { 88 System.out.println("第一个参数为读取文件路径,第二个参数为生成文件路径"); 89 if(args.length<1){ 90 System.out.println("请输入文件路径"); 91 return ; 92 }else if(args.length<2){ 93 System.out.println("请输入生成文件"); 94 return; 95 } 96 Change c = new Change(); 97 c.ReadFile(args[0]); 98 c.writerFile(args[1]); 99 } 100 }
以上是关于Java实验九第三题的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化