批量生成HTML文件,通过 学生名单(学号、姓名) 与 博客名单(姓名、网址)

构想

    很早就想写这个程序了,知道开学3周多了,才下定决心写完。

    功能:通过 学生名单(学号、姓名) 与 博客名单(姓名、网址),自动生成HTML文件。

    示例文件:学生名单博客名单

    结果文件(示例):HTML文件

运行结果

101 蔡伟权(缺)   102 曹翠芬       103 陈丹凤       104 陈桂强       105 陈家漫(缺)   

106 陈镜宇         108 陈满东         110 邓  辉(缺)     114 洪境鹏         115 黄  彬(缺)    
117 黄国滔(缺)     119 黄世安         120 黄世君(缺)     122 黄宇倩         124 江宗信(缺)    
125 赖长青(缺)     127 梁官荣         128 梁文俊         129 廖始聪(缺)     130 廖  鋆        
131 林  瀚         132 林骏琪         133 林镇填(缺)     134 刘  戈(缺)     135 刘威航        
136 陆志翔(缺)     139 倪粤鹏         142 容文山         143 容永豪(缺)     144 施恒亮        
145 谭永辉         146 韦林莹(缺)     148 吴辉平         150 吴郁鹏         151 吴韵杰(缺)    
152 谢碧君         153 谢舒韵         154 谢志杰(缺)     155 许翠怡(缺)     156 杨溢涛        
157 杨  媛(缺)     159 余文康         160 曾麒城         161 曾远辉         164 张玉婷        
169 庄树填(缺)    

使用方式

(a)命令行。 编译 javac HtmlBlog.java ;运行 java HtmlBlog wl131.html student_list_wl131.txt blog_list_wl131.txt

(b)Eclipse。 直接修改如下



Java源码

/**
 * (1)根据博客名单,自动生成HTML网页;每行5个数据
 * (2)判断博客地址是否符合要求,争取有纠错的功能
 * (3)判断有多少同学的博客没有,需要补充
 * 作者:丁又专
 * 时间:2014.03.26
 */
package indi.dyz.html;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlBlog {
	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */

	/**
	 * 功能:读取名单数据,保存到二维数组中
	 * 输入文件名:要求是两列数据,其中第一行为说明
	 * @param getList
	 * @return
	 * @throws FileNotFoundException
	 */
	public static String[][] getList(String fileName) throws FileNotFoundException{
		//判断文件是否存在
		File file = new File(fileName);
		if(file.exists()==false){
			System.out.println(fileName+"不存在,请检查该文件。");
			return null;
		}

		String strTmp, strLine;
		int numList = 0;
		//判断一共有多少行数据
		Scanner sc = new Scanner(file);
		while(sc.hasNextLine()){
			strTmp = sc.nextLine();	
			if(strTmp.length()>0){
				numList++;	//避免最后有空行
			}			
		}
		sc.close();
		numList = numList-1; //去掉首行说明

		String[][] studentList = new String[2][numList];
		int num = 0;
		//读名单数据
		sc = new Scanner(file);
		strTmp = sc.nextLine();		//过滤首行
		while(num<numList){
			strLine = sc.nextLine();
			//正则表达式进行分割,参考:字符串分割多个空格  http://blog.csdn.net/swarb/article/details/7402888
			String[] strArray = strLine.split("\\s{1,}");
			if(strArray.length==2) {
				studentList[0][num] = strArray[0];
				studentList[1][num] = strArray[1];
			}
			//System.out.println(num+" "+studentList[0][num] +":"+ studentList[1][num]);
			num++;
		}
		sc.close();

		return studentList;
	}

	/**
	 * 功能:判断数组中有哪些重复元素。
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static int getRepeat(String[] strArray){
		int numRepeat = 0;				//重复元素个数
		int numPre = 0, numLater = 0;	//插入元素前集合元素个数、插入后元素个数
		//把元素放到一个集合中,如何集合元素个数没有增加,则此元素重复
		HashSet<String> hs = new HashSet<String>();
		for(String str:strArray){
			hs.add(str);
			numLater = hs.size();
			if(numLater==numPre){
				numRepeat++;
				System.out.println("发现第"+numRepeat+"个重复元素:"+str+" 序号:"+numLater);
			}
			numPre = numLater;
		}
		return numRepeat;
	}

	/**
	 * 功能:找到两个集合(A,B)中的差异元素,即在A中,不在B中
	 * 思路:list1-list2,即集合的差,如A={1,2,3},B={1,3},则A-B={2}
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static String[] getDiffElement(String[] list1, String[] list2){
		HashSet<String> hs = new HashSet<String>();
		for(String strTmp:list2){
			hs.add(strTmp);
		}

		ArrayList<String> diffList = new ArrayList<String>(); 
		for(String strTmp:list1){
			if(hs.contains(strTmp)==false){
				diffList.add(strTmp);
			}
		}

		int num = diffList.size();
		if(num>0){
			String[] diff = new String[num];
			for(int i=0; i<num; i++){
				diff[i] = diffList.get(i);
			}	
			return diff;
		}	

		return null;
	}

	/**
	 * 功能:利用正则表达式,判断博客网址是否符合规范
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static boolean isCSDNBlog(String blog, String pattern){
		//http://blog.csdn.net/u013899770, true
		//http://write.blog.csdn.net/postlist, false
		//本例:pattern = "(http://blog.csdn.net/){1}";

		Pattern r = Pattern.compile(pattern);
		// 现在创建 matcher 对象
		Matcher m = r.matcher(blog);
		if (m.find( )) {
			//System.out.println(blog+" Found value: " + m.group(1) );
			return true;
		} else {
			//System.out.println("==="+blog+" NO MATCH");
			return false;
		}
	}

	/**
	 * 功能:写HTML文件,根据blog名单与学生名单,并输出没有找到的学生名单
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static int writeHtml(String fileHtml, String fileStudent, String fileBlog, String pattern) throws FileNotFoundException{
		//(1)创建HTML文件
		PrintWriter pw = new PrintWriter(new File(fileHtml));
		//(2)写HTML文件头
		pw.println("<HTML>");
		pw.println("<HEAD>");
		pw.println("	<TITLE>自动生成名单</TITLE>");
		pw.println("</HEAD>");
		pw.println("<BODY>");

		//(3)读取名单数据 与 博客数据
		String[][] studentList = getList(fileStudent);
		String[][] blogList = getList(fileBlog);

		//读取每一个学生名字,判断其博客地址是否正确
		//true:则保存到HTML文件
		//false:则在名字上标注一下(缺)
		String blankHtml = " ";
		String defaultUrl = "http://blog.csdn.net/dyz1982";
		String writeName = "";
		String writeUrl = "";

		int IDLen = studentList[0][0].length();

		int numStu = studentList[0].length;
		int numBlog = 0;
		int t=0, k=0;
		for(k=0; k<numStu; k++){
			//通过student名字,找到blog中网址
			String studentName = studentList[1][k].trim();
			for(t=0; t<blogList[0].length; t++){
				if(studentName.equalsIgnoreCase(blogList[0][t].trim()))
					break;
			}

			if(IDLen<=3){
				if(studentList[0][k].length()<2)
					writeName = blankHtml+studentList[0][k];
				else
					writeName = studentList[0][k];
			}
			else writeName = studentList[0][k].substring(IDLen-3);

			//判断姓名的字数,两个字的名字,中间加两个空格,为了美观
			if(studentName.length()==2){
				writeName += blankHtml + studentName.substring(0,1)+blankHtml+blankHtml+studentName.substring(1,2);
			}else if(studentName.length()>=3){
				writeName += blankHtml + studentName;
			}

			// blogList有地址,并且是符合规则的地址
			if( (t<blogList[0].length) && (isCSDNBlog(blogList[1][t],pattern)) ){	
				numBlog = numBlog+1;
				writeUrl = blogList[1][t];
				writeName = writeName + blankHtml+blankHtml+blankHtml+blankHtml;	//加4个空格,为了对齐
			}else{
				System.out.println(studentList[0][k]+studentName);

				writeUrl = defaultUrl;
				writeName = writeName + "(缺)";					
			}

			//写相应的HTML语句
			pw.print("<a target=_blank target=\"_blank\" href=\""+writeUrl+"\">"+writeName+"</a>");
			pw.println(blankHtml+blankHtml);
			//如果有5个人,则换行
			//最好的网络资源:http://www.w3school.com.cn/tags/tag_br.asp
			if((k+1)%5==0){
				pw.println("<br />");
			}
		}
		//写HTML文件尾
		/*
				</BODY>
				</HTML>
		 */
		pw.println("</BODY>"+"\n"+"</HTML>");
		pw.close();

		return numBlog;
	}

	/**
	 * 功能:对计科的博客名单进行重新处理
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void modifyJkBlogFile(String fileStudent, String fileBlog) throws FileNotFoundException{
		String[][] studentName = getList(fileStudent);
		String[][] studentBlog = getList(fileBlog);

		//先删除,再创建
		File file = new File(fileBlog);
		file.delete();
		file = new File(fileBlog);

		PrintWriter pw = new PrintWriter(file);
		//把“201111621321” 替换为相应的姓名
		//如果是201111621326吴叶英,则去掉前面的学号
		pw.println("姓名    网址");
		int i=0, j=0;
		for(i=0; i<studentBlog[0].length; i++){

			for(j=0; j<studentName[0].length; j++){
				if(studentBlog[0][i].contains(studentName[0][j]))break;
			}
			System.out.println(studentName[1][j]+"  "+studentBlog[1][i]);
			pw.println(studentName[1][j]+"  "+studentBlog[1][i]);
		}
		pw.close();
	}

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		String fileStudent;
		String fileBlog;
		String fileHtml;
		//查看命令行参数
		if(args.length==3){
			fileHtml = args[0];
			fileStudent = args[1];
			fileBlog = args[2];
		}else{
			fileHtml = "data/wl131.html";
			fileStudent = "data/student_list_wl131.txt";
			fileBlog = "data/blog_list_wl131.txt";
		}

		//读取文件数据
		String[][] studentName = getList(fileStudent);
		String[][] studentBlog = getList(fileBlog);

		//查看是否存在没有提交博客地址的同学
		String[] diffArray = getDiffElement(studentName[1], studentBlog[0]);
		for(String str:diffArray){
			System.out.println(str);
		}

		//查看博客地址是否准确,输出不准确的博客地址
		String pattern = "(http://blog.csdn.net/){1}[0-9a-zA-Z]+";	//模式字符串
		for(int i=0; i<studentBlog[1].length; i++){
			boolean b = isCSDNBlog(studentBlog[1][i], pattern);
			if(b==false) 
				System.out.println(studentBlog[0][i]+": "+studentBlog[1][i]);
		}

		//写HTML文件
		writeHtml(fileHtml, fileStudent, fileBlog, pattern);
		
		System.out.println("ok,end……");
	}
}










HTML是网页的基本构成元素,而PHP则是一种常见的服务器端编程语言。通过结合使用HTML和PHP,我们可以轻松实现从输入到输出的完整流程,实现对学生学号姓名的输入和输出。 我们可以从HTML表单开始,通过表单将学生学号姓名传递给PHP脚本进行处理。在HTML中,我们可以使用<form>标签来创建表单,同时使用<input>标签创建输入框,如下所示: <form method="post" action="input.php"> 学号:<input type="text" name="stu_no"><br> 姓名:<input type="text" name="stu_name"><br> <input type="submit" value="提交"> </form> 在这里,我们使用了method="post"和action="input.php"来指定请求方式和处理表单的PHP脚本的地址。同时,我们创建了两个输入框,一个是用于输入学号的,一个用于输入姓名的。在最后,我们使用<input>标签创建提交按钮,供用户提交表单信息。 在PHP脚本中,我们可以使用$_POST数组来获取表单中的数据,并进行处理。例如,我们可以使用如下代码来获取学号姓名: $stu_no = $_POST['stu_no']; $stu_name = $_POST['stu_name']; 接下来,我们可以使用echo语句将学号姓名输出到HTML页面上。例如: echo "学号:" . $stu_no . "<br>"; echo "姓名:" . $stu_name . "<br>"; 通过以上的代码,我们就完成了从输入到输出的完整流程,实现了对学生学号姓名的输入和输出。当用户在HTML表单中输入学号姓名并提交后,PHP代码将自动处理,最后将结果输出到HTML页面中。 总之,通过HTML和PHP的结合使用,我们可以实现丰富多彩的网页交互效果,并大大提高了网站用户体验和安全性。对于学生学号姓名的输入输出,也是同样的道理,可以轻松实现这一功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值