Jsoup. jar package to be introduced: jsoup. Jar
Recommended jsoup Tutorial: Use jsoup to process HTML documents
The following is the Java code that implements jsoup parsing HTML files and saves them to the local machine:
/*** This file implements: Replace the <title> tag value of all htm and HTML files in the specified directory with the file name (excluding the suffix ). */Import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstreamwriter; import Org. jsoup. jsoup; import Org. jsoup. nodes. document; import Org. jsoup. nodes. element; public class rename {public static void main (string [] ARGs) {// Default Folder path string Path = "C: \ Report"; if (ARGs! = NULL & args. length> 0) {Path = ARGs [0];} Try {renamehtmltitle (PATH);} catch (ioexception e) {e. printstacktrace () ;}} public static void renamehtmltitle (string DIR) throws ioexception {file F = new file (DIR); If (F. isdirectory () {file FS [] = f. listfiles (); For (File S: FS) {String title = S. getname (). replaceall (". htm ",""). replaceall (". html "," "); If (S. getname (). contains (". htm ") | S. getname (). contai NS (". html ") {document DOC = jsoup. parse (S, "gb2312"); element titleel = Doc. select ("title" ).first();titleel.html (title);/** jsoup is only parsed and cannot be saved. Therefore, save the changes here. */Fileoutputstream Fos = new fileoutputstream (S, false); outputstreamwriter OSW = new outputstreamwriter (FOS, "gb2312" wrote into osw.write(doc.html (); OSW. Close ();}}}}}