导航:首页 > 编程语言 > java生成html文件

java生成html文件

发布时间:2022-09-08 04:08:24

A. 编写程序,将一个java文件转换为HTML一个文件

java中将java文件转换为html一个文件,先使用file类读取java文件,然后使用string进行分割、替换等操作,输出html后缀名的文件,如下代码:

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStreamReader;

publicclassChange{
StringtextHtml="";
Stringcolor="#00688B";
//读取文件
publicvoidReadFile(StringfilePath){
BufferedReaderbu=null;
InputStreamReaderin=null;
try{
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){
in=newInputStreamReader(newFileInputStream(file));
bu=newBufferedReader(in);
StringlineText=null;
textHtml="<html><body>";
while((lineText=bu.readLine())!=null){
lineText=changeToHtml(lineText);
lineText+="</br>";
textHtml+=lineText;
}
textHtml+="</html></body>";
}else{
System.out.println("文件不存在");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
bu.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

//输出文件
publicvoidwriterFile(Stringwritepath){
Filefile=newFile(writepath);
BufferedWriteroutput=null;
try{
output=newBufferedWriter(newFileWriter(file));
System.out.println(textHtml);
output.write(textHtml);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
output.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

//文件转换
publicStringchangeToHtml(Stringtext){
text=text.replace("&","&");
text=text.replace("","");
text=text.replace("<","<");
text=text.replace(">",">");
text=text.replace(""",""");
text=text.replace("","");
text=text.replace("public","<b><fontcolor='"+color+"'>public</font></b>");
text=text.replace("class","<b><fontcolor='"+color+"'>class</font></b>");
text=text.replace("static","<b><fontcolor='"+color+"'>static</font></b>");
text=text.replace("void","<b><fontcolor='"+color+"'>void</font></b>");
Stringt=text.replace("//","<fontcolor=green>//");
if(!text.equals(t)){
System.out.println("t:"+t);
text=t+"</font>";
}
returntext;
}

publicstaticvoidmain(String[]args){
System.out.println("第一个参数为读取文件路径,第二个参数为生成文件路径");
if(args.length<1){
System.out.println("请<ahref="https://www..com/s?wd=%E8%BE%93%E5%85%A5%E6%96%87%E4%BB%B6&tn=44039180_cpr&fenlei=_5y9YIZ0lQzqlpA-"target="_blank"class="-highlight">输入文件</a>路径");
return;
}elseif(args.length<2){
System.out.println("请输入生成文件");
return;
}
Changec=newChange();
c.ReadFile(args[0]);
c.writerFile(args[1]);
}
}

B. 如何在java中实现自动生成html

自动生成?
1,编写html文件的内容
2,将编写的文件内容写入一个文件中
3,保存文件为.html文件到指定目录
4,根据路径调用生成的html文件

C. 谁做过java自动生成html 原理讲下

大概就是一楼的那个意思 给你个小例子你看下;
先创建一个html模板:
<html>
<head>
<title>###title###</title>
<meta http- equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head>
<body>
<table width="500" border="0" align="center" cellpadding="0"
cellspacing="2">
<tr>
<td align="center">
###title###
</tr>
<tr>
<td align="center">
作者:###author###
</tr>
<tr>
<td align="center">
###content###
</td>
</tr>
</table>
</body>
</html>

java代码
import java.util.*;
import java.io.*;

public class HtmlFile {
public static void main(String[] args) {
try {
String title = "Make Html";
String content = "小样,还搞不定你?";
String editer = "秋水";
//模板路径
String filePath = "leon.html";
System.out.print(filePath);
String templateContent = "";
FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
System.out.print(templateContent);
templateContent = templateContent.replaceAll("###title###", title);
templateContent = templateContent.replaceAll("###content###",
content);
templateContent = templateContent
.replaceAll("###author###", editer);// 替换掉模板中相应的地方
System.out.print(templateContent);

// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis())
+ ".html";
fileame = "/" + fileame;// 生成的html文件保存路径。
FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
System.out.print("文件输出路径:");
System.out.print(fileame);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
} catch (Exception e) {
System.out.print(e.toString());
}
}
}

D. java中怎么将数据库中的数据拿出并生成一个HTML文件。

您好,这简单啊,你自己先创建一个mdb文件啊,连接上mdb文件,然后向他中创建表,再插入数据就OK:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db1.mdb";
Connection con= DriverManager.getConnection(url,"user","pass");

E. java动态的生成html页面

ArrayList al,获取这张表的信息
定义一个TableName类,TableName tn=new TableName();
<%
for(int i=0;i<al.size();i++){
tn=al.get(i);
%>
<tr>
<td>tn.getname()</td>
.......
</tr>
<%
}
%>

F. JAVA动态生成HTML代码

freemarker没有功能。freemarker要求你自己把东西取出来放到request的attribute里面,然后在ftl文件中使用那些attribute。

可以自己用Java反射来得到实体中字段,然后自己拼html字符串

G. Java 数据生成html

不太懂thymeleaf把数据放在session中是什么意思。
使用thymeleaf的效率是肯定比前台渲染高的,因为你请求直接返回的就是后台已经处理好的html文档,不再需要发第二次请求获取数据,再使用js进行渲染操作。
但是页面加载速度反而使用js的方式更快,因为他可以先渲染出页面,再去加载数据。

H. 如何用java代码在本地硬盘里生成一个html页面

创建一个StringBuilder对象,通过append方法来为其添加html语句。
StringBuilder sb = new StringBuilder();
Properties fileProperties = getProperties("file");
Properties sqlProperties = getProperties("sql");
PrintStream printStream = new PrintStream(new FileOutputStream(
"report.html"));
sb.append("<html>");
sb.append("<head>");
sb.append("<title>每日运营报表</title>");
sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
sb.append("<style type=\"text/css\">");
sb.append("TABLE{border-collapse:collapse;border-left:solid 1 #000000; border-top:solid 1 #000000;padding:5px;}");
sb.append("TH{border-right:solid 1 #000000;border-bottom:solid 1 #000000;}");
sb.append("TD{font:normal;border-right:solid 1 #000000;border-bottom:solid 1 #000000;}");
sb.append("</style></head>");
sb.append("<body bgcolor=\"#FFF8DC\">");
sb.append("<div align=\"center\">");
sb.append("<br/>");
sb.append("<br/>");
List<Map<String, Object>> result1 = getRpt(sqlProperties
.getProperty("sql1"));
for (Map.Entry<String, Object> m : result1.get(0).entrySet()) {
sb.append(fileProperties.getProperty("file1"));
sb.append(m.getValue());
}
sb.append("<br/><br/>");
输出
sb.append("</div></body></html>");
printStream.println(sb.toString());

I. 编写程序,将一个Java文件转换为HTML一个文件.

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStreamReader;

publicclassChange{
StringtextHtml="";
Stringcolor="#00688B";
//读取文件
publicvoidReadFile(StringfilePath){
BufferedReaderbu=null;
InputStreamReaderin=null;
try{
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){
in=newInputStreamReader(newFileInputStream(file));
bu=newBufferedReader(in);
StringlineText=null;
textHtml="<html><body>";
while((lineText=bu.readLine())!=null){
lineText=changeToHtml(lineText);
lineText+="</br>";
textHtml+=lineText;
}
textHtml+="</html></body>";
}else{
System.out.println("文件不存在");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
bu.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

//输出文件
publicvoidwriterFile(Stringwritepath){
Filefile=newFile(writepath);
BufferedWriteroutput=null;
try{
output=newBufferedWriter(newFileWriter(file));
System.out.println(textHtml);
output.write(textHtml);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
output.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

//文件转换
publicStringchangeToHtml(Stringtext){
text=text.replace("&","&amp;");
text=text.replace("","&nbsp;");
text=text.replace("<","&lt;");
text=text.replace(">","&gt;");
text=text.replace(""","&quot;");
text=text.replace("","&nbsp;&nbsp;&nbsp;&nbsp;");
text=text.replace("public","<b><fontcolor='"+color+"'>public</font></b>");
text=text.replace("class","<b><fontcolor='"+color+"'>class</font></b>");
text=text.replace("static","<b><fontcolor='"+color+"'>static</font></b>");
text=text.replace("void","<b><fontcolor='"+color+"'>void</font></b>");
Stringt=text.replace("//","<fontcolor=green>//");
if(!text.equals(t)){
System.out.println("t:"+t);
text=t+"</font>";
}
returntext;
}

publicstaticvoidmain(String[]args){
System.out.println("第一个参数为读取文件路径,第二个参数为生成文件路径");
if(args.length<1){
System.out.println("请输入文件路径");
return;
}elseif(args.length<2){
System.out.println("请输入生成文件");
return;
}
Changec=newChange();
c.ReadFile(args[0]);
c.writerFile(args[1]);
}
}


传参调用:



楼主,根据自己的实际需要,再调整下吧

J. 如何用java生成html文件

不是很明白你的需求。

这么说吧,要想生成html页面的话,容器会替我们直接把jsp编译成servlet输出成html静态页面进行展示。
你要像手动输出html的展示内容可以自己写一个servlet,使用output方法输出html标签代码段直接打印到客户端。
还有如果你想写入html文件的话,你可以通过fileinput字节写入。(这种写法servlet教程上很多实例,包括如何生成文件,如何通过字节或者字符流的形式写入和保存)
那么你问的是哪一种呢?

阅读全文

与java生成html文件相关的资料

热点内容
美食博主用什么app拍视频 浏览:812
ipone手机如何加密微信 浏览:354
自来水加密阀阀帽 浏览:431
华为交换机dhcp配置命令 浏览:315
androidbitmap缩小 浏览:271
单片机串口控制灯 浏览:84
大讯云服务器安装视频 浏览:784
华为算法领先世界 浏览:654
linux路由重启 浏览:566
php的模板编程 浏览:320
编译器原理与实现书 浏览:709
dos选择命令 浏览:17
apm固件编译到单片机 浏览:121
联通深蓝卡都包含什么app 浏览:264
如何判断网络服务器正常 浏览:652
路由器搭桥远端服务器地址是什么 浏览:518
编译动态库时会连接依赖库吗 浏览:710
淘宝手机加密是随机的吗 浏览:675
解压包子怎么装饰 浏览:588
四个数凑24算法 浏览:679