将一个java文件转换为html文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Change {
    String textHtml = "";
    String color = "#00688B";
    //读取文件
    public void ReadFile(String filePath) {
        BufferedReader bu = null;
        InputStreamReader in = null;
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                in = new InputStreamReader(new FileInputStream(file));
                bu = new BufferedReader(in);
                String lineText = null;
                textHtml = "<html><body>";
                while ((lineText = bu.readLine()) != null) {
                    lineText = changeToHtml(lineText);
                    lineText += "</br>";
                    textHtml += lineText;
                }
                textHtml += "</html></body>";
            } else {
                System.out.println("文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bu.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    //输出文件
    public void writerFile(String writepath) {
        File file = new File(writepath);
        BufferedWriter output = null;
        try {
            output = new BufferedWriter(new FileWriter(file));
            System.out.println(textHtml);
            output.write(textHtml);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    //文件转换
    public String changeToHtml(String text) {
        text = text.replace("&", "&");
        text = text.replace(" ", " ");
        text = text.replace("<", "<");
        text = text.replace(">", ">");
        text = text.replace("\"", """);
        text = text.replace(" ", "    ");
        text = text.replace("public", "<b><font color='"+color+"'>public</font></b>");
        text = text.replace("class", "<b><font color='"+color+"'>class</font></b>");
        text = text.replace("static", "<b><font color='"+color+"'>static</font></b>");
        text = text.replace("void", "<b><font color='"+color+"'>void</font></b>");
        String t = text.replace("//", "<font color=green>//");
        if (!text.equals(t)) {
            System.out.println("t:"+t);
            text = t + "</font>";
        }
        return text;
    }
 
    public static void main(String[] args) {
        System.out.println("第一个参数为读取文件路径,第二个参数为生成文件路径");
        if(args.length<1){
            System.out.println("请输入文件路径");
            return ;
        }else if(args.length<2){
            System.out.println("请输入生成文件");
            return;
        }
        Change c = new Change();
        c.ReadFile(args[0]);
        c.writerFile(args[1]);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值