展开全部
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) {
62616964757a686964616fe78988e69d8331333337373561BufferedReader 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 = "
";while ((lineText = bu.readLine()) != null) {
lineText = changeToHtml(lineText);
lineText += "";
textHtml += lineText;
}
textHtml += "";
} 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", "public");
text = text.replace("class", "class");
text = text.replace("static", "static");
text = text.replace("void", "void");
String t = text.replace("//", "//");
if (!text.equals(t)) {
System.out.println("t:"+t);
text = t + "";
}
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]);
}
}
传参调用:
运行结果:
楼主,根据自己的实际需要,再调整下吧