Generate HTML files in batches, and use the student list (student ID, name) and blog list (name, URL)

Source: Internet
Author: User

Concept

I wanted to write this program for a long time. I knew that I had been in school for more than three weeks before I made up my mind to write it.

Function: automatically generates HTML files through the student list (student ID, name) and blog list (name, URL.

Example file: student list and blog list.

Result file (example): HTML file.

Running result

101 Cai weiquan (lack) 102 Cao cuifen 103 Chen Danfeng 104 Chen guiqiang 105 Chen jiaman (lack)

106 Chen Jingyu 108 Chen mandong 110 Deng Hui (lack) 114 Hong Jing Peng 115 Huang Bin (lack)
117 Huang guoyu (lack) 119 Huang Shian 120 Huang Shijun (lack) 122 Huang yuqian 124 Jiang zongxin (lack)
125 Lai Changqing (lack) 127 Liang guanrong 128 Liang Wenjun 129 Liao shicong (lack) 130 Liao Yu
131 Lin Han 132 Lin Junqi 133 Lin Zhen filling (missing) 134 Liu Ge (missing) 135 Liu weihang
136 Lu zhixiang (lack) 139 Lu yuepeng 142 Rong Wenshan 143 Rong Yonghao (lack) 144 Shi hengliang
145 tan Yonghui 146 Ma Ying (missing) 148 Wu huiping 150 Wu Yupeng 151 Wu yunjie (missing)
152 Xie Bijun 153 Xie Shuyun 154 Xie Zhijie (missing) 155 Xu cuiyi (missing) 156 Yang yitao
157 Yang Yuan (lack) more than 159 Wen Kang 160 Zeng xiaocheng 161 Zeng yuanhui 164 Zhang Yuting
169 Zhuang Shu filling (missing)

Usage

(A) command line. Compile javac HtmlBlog. java; run java HtmlBlog wl131.html student_list_wl131.txt blog_list_wl131.txt

(B) Eclipse. Directly modify the following:



Java source code

/*** (1) automatically generate HTML webpages Based on the blog list; 5 data entries per row * (2) Determine whether the blog address meets the requirements and strive for the error correction function * (3) to determine the number of blog posts not available, add * Author: Ding youzhuan * Time: 4.04.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. imports; import java. util. regex. matcher; import java. util. regex. pattern; public class HtmlBlog {/*** @ param args * @ t Hrows FileNotFoundException * // *** function: Read the list data and save it to a two-dimensional array. * input file name: two columns of data are required, the first behavior description * @ param getList * @ return * @ throws FileNotFoundException */public static String [] [] getList (String fileName) throws FileNotFoundException {// determines whether a File exists. file = new File (fileName); if (file. exists () = false) {System. out. println (fileName + "does not exist. Check the file. "); Return null;} String strTmp, strLine; int numList = 0; // you can determine the total number of rows of data in the queue SC = new Queue (file); while (SC. hasNextLine () {strTmp = SC. nextLine (); if (strTmp. length ()> 0) {numList ++; // avoid the last empty row} SC. close (); numList = numList-1; // remove the first line description String [] [] studentList = new String [2] [numList]; int num = 0; // read list data SC = new partition (file); strTmp = SC. nextLine (); // filter the first line while (num <numList) {strLine = SC. nextLine (); // Regular Expression for segmentation For details, refer to: Separate multiple spaces in a string: http://blog.csdn.net/swarb/article/details/7402888string#] 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;}/*** function: determines which repeated elements exist in the array. * @ Param args * @ throws FileNotFoundException */public static int getRepeat (String [] strArray) {int numRepeat = 0; // number of repeated elements int numPre = 0, numLater = 0; // The number of elements in the set before the element is inserted, and the number of elements after the element is inserted. // put the element into a set. How can the number of elements in the set not increase, this element repeats HashSet <String> hs = new HashSet <String> (); for (String str: strArray) {hs. add (str); numLater = hs. size (); if (numLater = numPre) {numRepeat ++; System. out. println ("detected" + numRepeat + "repeated element:" + str + "No.:" + numLater );} NumPre = numLater;} return numRepeat;}/*** function: locate the difference element in the two sets (A, B), that is, in, not in B * idea: list1-list2, that is, set the difference, such as A = {1, 2, 3}, B = {1, 3 }, then the 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 strTm P: 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;}/*** function: Use a regular expression, determine whether the blog URL complies with the specifications * @ param args * @ throws FileNotFoundException */public static boolean isCSDNBlog (String blog, String pattern) {// http://blog.csdn.net/u013899770, true // http: // wri Te.blog.csdn.net/postlist, false // example: pattern = "(http://blog.csdn.net/) {1}"; Pattern r = Pattern. compile (pattern); // now create the matcher object 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;}/*** function: write HTML files. Based on the blog list and student list, output The unfound student list * @ param args * @ throws FileNotFoundExcepti On */public static int writeHtml (String fileHtml, String fileStudent, String fileBlog, String pattern) throws FileNotFoundException {// (1) create the HTML File PrintWriter pw = new PrintWriter (new File (fileHtml); // (2) write the HTML File header pw. println ("<HTML>"); pw. println ("<HEAD>"); pw. println ("<TITLE> automatically generated list </TITLE>"); pw. println ("</HEAD>"); pw. println ("<BODY>"); // (3) read list data and blog data String [] [] studentList = getList (fileStudent); Stri Ng [] [] blogList = getList (fileBlog); // read the name of each student and check whether the blog address is correct. // true: Save it to the HTML file // false: mark (missing) 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 ++) {// use the student name, find the URL 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]; elsewriteName = studentList [0] [k];} else writeName = studentList [0] [k]. substring (IDLen-3); // determines the number of words in the name, the name of the two words, with two spaces in the middle, for the beautiful if (studentName. length () = 2) {writeName + = blankHtml + studentName. substring (0, 1) + balancer HTML + BlankHtml + studentName. substring (1, 2);} else if (studentName. length ()> = 3) {writeName + = blankHtml + studentName;} // blogList has an address and is a compliant address if (t <blogList [0]. length) & (isCSDNBlog (blogList [1] [t], pattern) {numBlog = numBlog + 1; writeUrl = blogList [1] [t]; writeName = writeName + balankhtml + balancer HTML; // Add four spaces for alignment} else {System. out. println (studentList [0] [k] + studentName); writeUrl = defaultU Rl; writeName = writeName + "(missing)" ;}// write the corresponding HTML statement pw. print ("<a target = _ blank target = \" _ blank \ "href = \" "+ writeUrl +" \ ">" + writeName + "</a> "); pw. println (blankHtml + blankHtml); // if there are 5 people, line feed // The Best Network Resources: http://www.w3school.com.cn/tags/tag_br.aspif (k + 1) % 5 = 0) {pw. println ("<br/>") ;}// write the end Of the HTML file/* </BODY> </HTML> */pw. println ("</BODY>" + "\ n" + "</HTML>"); pw. close (); return numBlog;}/*** function: reprocesses the blog list of the Family Planning Department * @ param args * @ Throws FileNotFoundException */public static void modifyJkBlogFile (String fileStudent, String fileBlog) throws FileNotFoundException {String [] [] studentName = getList (fileStudent ); string [] [] studentBlog = getList (fileBlog); // delete the File first, and then create file File = new file (fileBlog); File. delete (); file = new File (fileBlog); PrintWriter pw = new PrintWriter (file); // Replace "201111621321" with the corresponding name // if it is 201111621326 Wu ye Ying, remove the previous student ID pw. Println ("name URL"); 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 stubString f IleStudent; String fileBlog; String fileHtml; // view the command line parameter 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";} // read file data String [] [] studentName = getList (fileStudent ); string [] [] studentBlog = getList (fileBlog); // you can check whether a student who has not submitted a blog address is found. String [] diffArray = getDiffElement (StudentName [1], studentBlog [0]); for (String str: diffArray) {System. out. println (str);} // check whether the blog address is accurate, output the inaccurate blog address String pattern = "(http://blog.csdn.net/) {1} [0-9a-zA-Z] + "; // mode string 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]);} // write the HTML file writeHtml (fileHtml, fileStudent, fileBlog, pattern ); System. out. println ("OK, end ...... ");}}










Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.