根据txt内容生成html博客名单

今天用java粗略实现了丁又专老师之前在博客发表的题目--“附加题及示例代码(写HTML文件):根据给定的博客名单,自动生成HTML网页,完成后回看丁老师发布的实现代码--“批量生成HTML文件,通过 学生名单(学号、姓名) 与 博客名单(姓名、网址)”,看到自己对写代码的规范要求实在太不严格了,感觉自己是在针对实现个别问题而完成工作,而不是针对类别问题而去实现。


参考博客名单资料:blog_list_wl131.txt若有冒犯之处,还请及时联系,谢谢。


往后应以此为鉴,多去思考如何解决一类问题,而不是解决一个问题。


既然写了出来,把结果呈现,并以此提醒自己。此程序读取的txt文件格式只能是参考格式,默认跳过文件第一行不读取。待完善功能:自定义跳过行数,博客数据的格式自定义(采用正则式匹配)。


生成的html博客列表名单如下:


CSDN博客列表
   
陈鹏林炳权谢锐鹏魏凯毅谈健安
李家华薛伟良杨洁莹廖志伟黄杰
郑楷山李芷婷陈培凯林开辉郭海燕
庄勤达何志云陈剑洪梁业锋李钊
黎家成李晓毅蔡勇涛黄泽茂陈思颖
温明玉刘松浩沈鸿伟张金生苏文斌
王金泽

代码展示:

1.FileChoice.java

package txt2html;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileChoice extends JFrame{

	private String path = null;//源文件所在路径
	private String outHtmlName = null;//生成文件路径+文件名
	
	public FileChoice(){
		JPanel center = new JPanel();
		center.setLayout(null);
		
		JLabel sourceName = new JLabel("源文件:");
		JLabel fileName = new JLabel("生成的网页文件名:");
		final JTextField sourceNameInput = new JTextField();
		final JTextField fileNameInput = new JTextField();
		sourceName.setBounds(30,20,50, 30);
		sourceNameInput.setBounds(90,23,160,22);
		fileName.setBounds(30,60,130, 30);
		fileNameInput.setBounds(150,63,100,22);
		sourceNameInput.setEditable(false);//源文件路径不可编辑
		
		JButton chooseTxt = new JButton("选择文件"); 
		JButton confirm = new JButton("确定");
		JButton cancel = new JButton("取消"); 
		chooseTxt.setBounds(30,110,100, 30);
		confirm.setBounds(140,110,60, 30);
		cancel.setBounds(210,110,60, 30);
		
		
		//选择文件按钮监听
		chooseTxt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0){
				//弹出文件选择对话框
				JFileChooser c = new JFileChooser();
				c.setDialogTitle("请选择博客名单源文件");//标题栏
				int result = c.showOpenDialog(null);
				//点击确定按钮
				if(JFileChooser.APPROVE_OPTION == result) {
					FileChoice.this.path = c.getSelectedFile().getPath();//获取选择文件的路径	
					sourceNameInput.setText(FileChoice.this.path);
					outHtmlName = sourceNameInput.getText().toString().trim();
				}	
			}
		});
		
		//确定按钮监听
		confirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					//源文件不能为空
					if(sourceNameInput.getText().toString().trim().length()==0)
						JOptionPane.showMessageDialog(FileChoice.this,"源文件名错误,请重新选择。");
					//生成文件名不能为空
					else if(fileNameInput.getText().toString().trim().length()==0)
						JOptionPane.showMessageDialog(FileChoice.this,"请输入生成文件名。");
					else{
						//截取路径,除去源文件名称和扩展名
						outHtmlName = outHtmlName.substring(0,outHtmlName.lastIndexOf("\\"))+"\\";
						outHtmlName = outHtmlName+fileNameInput.getText().toString().trim()+".html";
						new Out2Html(sourceNameInput.getText().toString().trim(),outHtmlName);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				fileNameInput.setText("");
			}
		});
		
		//取消按钮监听
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		center.add(sourceName);
		center.add(sourceNameInput);
		center.add(fileName);
		center.add(fileNameInput);
		center.add(chooseTxt);
		center.add(confirm);
		center.add(cancel);
		
		this.setTitle("生成blog网页名册");
		this.add(center,BorderLayout.CENTER);
		this.setVisible(true);
		this.setBounds(300, 200, 300, 200);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new FileChoice();
	}
}


2.Out2Html.java

package txt2html;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Out2Html extends JFrame{
	/**
	 * @param path 源文件路径
	 * @param outHtmlName 输出文件名
	 */
	//源文件名单文件路径,格式:名称+空格+博客地址。首行标题忽略。
	public Out2Html(String path,String outHtmlName) throws Exception{
		File file = new File(outHtmlName);
		PrintStream outFile = null;
		//存在文件名相同则取消
		if(file.exists()){
			JOptionPane.showMessageDialog(this,"已存在文件:"+outHtmlName);
		}
		else{
			outFile = new PrintStream(file);
			
			String lineData = null;
			StringBuffer web = new StringBuffer();//字符串,用于设置页面内容
			
			web.append("<!DOCTYPE html PUBLIC "+"-//W3C//DTD XHTML 1.0 Transitional//EN");
			web.append("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"+">");
			web.append("<html xmlns="+"http://www.w3.org/1999/xhtml"+">");
			web.append("<head>");
			web.append("<title>studentList</title>");
			web.append("<style type="+"text/css"+">");
			web.append(".name {font-size: 16px; font-weight: bold;}");
			web.append("a:link {color: #00F;");
			web.append("text-decoration: none}");
			web.append("a:visited {color: #00F;");
			web.append("text-decoration: none}");
			web.append("a:hover {color: #09C;");
			web.append("text-decoration: none}");
			web.append("a:active {color: #09C;");
			web.append("text-decoration: none}");
			web.append("</style>");
			web.append("</head>");
			web.append("<body><table width='600' border='0' align='center' style='margin-top:100px;'>");
			web.append("<caption height='80' align='center' valign='middle'>CSDN博客列表</caption>");
			web.append("<tr height='30'><td></td><td></td><td></td></tr><tr>");
	
			//得到文件,并用输入流读取文件内容
			File inputFile = new File(path);
			InputStreamReader read = new InputStreamReader(new FileInputStream(inputFile), "GBK");
			BufferedReader bufferedReader = new BufferedReader(read);
			String[] user = new String[2];//获取每一行数据
			bufferedReader.readLine();//文档首行标题不要
				
			//从第二行开始读起
			while((lineData = bufferedReader.readLine()) != null){  
				String[] info = lineData.split("\\s+");
				for(int n=0;n<info.length;n++){
					user[n] = info[n];
				}
			web.append("<tr height='40'><td align='center' valign='middle'><a href='"+user[1]+"'>"+user[0]+"</a></td>");
					   
				if((lineData = bufferedReader.readLine())!=null){ //换行获取数据
					info = lineData.split("\\s+");
					for(int n=0;n<info.length;n++){
						user[n] = info[n];
					}
					web.append("<td align='center'><a href='"+user[1]+"'>"+user[0]+"</a></td>");
				}
					   
				if((lineData = bufferedReader.readLine())!=null){ //换行获取数据
					info = lineData.split("\\s+");
					for(int n=0;n<info.length;n++){
						user[n] = info[n];
					}
					web.append("<td align='center'><a href='"+user[1]+"'>"+user[0]+"</a></td>");
				}
					   
				if((lineData = bufferedReader.readLine())!=null){ //换行获取数据
					info = lineData.split("\\s+");
					for(int n=0;n<info.length;n++){
						user[n] = info[n];
					}
					web.append("<td align='center'><a href='"+user[1]+"'>"+user[0]+"</a></td>");
				}
					   
				if((lineData = bufferedReader.readLine())!=null){ //换行获取数据
					info = lineData.split("\\s+");
					for(int n=0;n<info.length;n++){
						user[n] = info[n];
					}
					web.append("<td align='center'><a href='"+user[1]+"'>"+user[0]+"</a></td></tr>");
				}
			}
			web.append("</table></body></html>");
			outFile.println(web.toString());//输出html文件
			read.close();
			outFile.close();
			JOptionPane.showMessageDialog(this,"成功生成文件:"+outHtmlName);
		}
	}
}


测试结果:

1.


2.

3.


4.


5.


6.


7.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值