Common methods for URL classes in JAVA __java

Source: Internet
Author: User
Tags readline

Common methods in the 1.URL class:

Create a URL object
    url url = new URL ("http://www.baidu.com");  
    URL url1 = new URL (url, "/index.html?usrname=lqq#test");
Common methods for URLs
    System.out.println ("URL host name:" +url1.gethost ());
    System.out.println ("URL protocol:" +url1.getprotocol ());
    System.out.println ("URL port:" +url1.getport ());
    System.out.println ("URL file path:" +url1.getpath ());
    System.out.println ("URL query string:" +url1.getquery ());
    System.out.println ("url relative path:" +url1.getref ());
    SYSTEM.OUT.PRINTLN ("URL filename:" +url1.getfile ());   

2. Get HTML file content via URL:

//create a URL instance url url = new URL ("http://www.baidu.com");
Gets a InputStream object InputStream is = Url.openstream () through the OpenStream method of the URL object;
InputStreamReader ISR = new InputStreamReader (IS, "utf-8");

BufferedReader br = new BufferedReader (ISR);
FILE BD = new file (".../baidu.html");
PrintWriter pw = new PrintWriter (BD);
Reads String s = Br.readline () by row;
    while (S!=null) {pw.print (s);
    Pw.flush ();
S=br.readline ();
//Close Related resources pw.close ();
Br.close ();
Isr.close ();
Is.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.