Parse HTML files with jsoup and save them to a local device

Source: Internet
Author: User

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 ();}}}}}

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.