Java-url class explanation

Source: Internet
Author: User

The class URL represents a Uniform resource locator, which is a pointer to the Internet "resource". A resource can be a simple file or directory, or a reference to a more complex object, such as a query to a database or search engine.

URL (String spec)              int port, String file)             Creates a URL object based on the specified protocol, host, port number, and file. 

URL Sample code:

getFile ()                         Gets the file name for this URL. String gethost ()             gets the hostname of this URL (if applicable). String GetPath ()             int  getport ()                   obtains the port number for this URL. String Getprotocol ()        obtains the protocol name for this URL. OpenConnection () returns a URLConnection object that represents the connection to the remote object referenced by the URL. 

eg

Importjava.net.*; Public classUrldemo { Public Static voidMain (string[] args) {//Urldemo Urldemo = new Urldemo ();              Try{URL hp=NewURL ("http://www.hao123.com/"); System.out.println ("Protocol:" +Hp.getprotocol ()); System.out.println ("Port:" +Hp.getport ()); System.out.println ("Host:" +hp.gethost ()); System.out.println ("File:" +hp.getfile ()); System.out.println ("EXT:" +hp.toexternalform ()); } Catch(Malformedurlexception ex) {System.out.println (ex.tostring ()); }      }}

Output:

-1/ext:http://www.hao123.com/

Java-url class explanation

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.