码迷,mamicode.com
首页 > 编程语言 > 详细

Java实验九第三题

时间:2020-05-13 09:47:06      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:生成   stack   文件路径   tac   NPU   equal   ref   gre   new   

答案我也是嫖的!

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实验九第三题

标签:生成   stack   文件路径   tac   NPU   equal   ref   gre   new   

原文地址:https://www.cnblogs.com/angoli/p/12880169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!