Java Learning Note (58)-inetaddress class and URL

Source: Internet
Author: User

InetAddress class
/ * * InetAddress class * /public class Test07 {public static void main (string[] args) throws Unknownhostexception {//Get native InetAddress instance System. out. println("******* Gets the inetaddress instance of this machine **********");InetAddress Address1 = inetaddress. Getlocalhost();System. out. println("host name:"+address1. GetHostName());System. out. println("IP address:"+address1. Gethostaddress());Byte[] Bytes=address1. GetAddress();System. out. println("IP address as a byte array:"+arrays. toString(bytes));//More than 127 is a negative number, plusSystem. out. println(ADDRESS1);Gets the InetAddress instance System for the specified host based on the host name. out. println("******* Gets the InetAddress instance of the specified host based on the hostname **********");InetAddress address2=inetaddress. Getbyname("He67b6djboid2da");InetAddress address2=inetaddress. Getbyname("5.8.6.45");System. out. println(Address2. GetHostName());System. out. println(ADDRESS2);Gets the InetAddress instance System based on an IP address in the form of a byte array. out. println("********** Gets an InetAddress instance based on an IP address in the form of a byte array ***********");Byte[] bytes2={(byte)192, (Byte)168,6,8};InetAddress address3=inetaddress. Getbyaddress(Bytes2);System. out. println(ADDRESS3. GetHostName());System. out. println(ADDRESS3);}}
URL Common methods
/ * * URL Common method */public class Test08 {public static void main (string[] args) throws malformedurlexception {URL baidu=new url ("Http://www.baidu.com");URL url=new url (Baidu,"/index.html?username=tom&password=123#test");System. out. println("agreement:"+url. Getprotocol());System. out. println("Host:"+url. GetHost());System. out. println("Port:"+url. Getport());System. out. println("File path:"+url. GetPath());System. out. println("file name:"+url. GetFile());System. out. println("relative path:"+url. GetRef());System. out. println("Query:"+url. Getquery());}}
Using URLs to read Web page content
Import Java.io.bufferedinputstream;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.malformedurlexception;import Java.net.URL;/ * * Use URL to read Web content */ Public classTest01 { Public Static void Main(string[] args) throws IOException {test02 (); }//Read Web resources     Public Static void test01() throws ioexception{url url =NewURL ("http://localhost:8080/webTest/"); InputStream is= Url.openstream ();//Gets the input stream of the resource represented by the URL objectBufferedReader br =NewBufferedReader (NewInputStreamReader ( is));//Convert byte input stream to character input streamString str = br.readline (); while(str! =NULL) {System. out. println (str);        str = Br.readline (); } br.close (); is. Close (); }//Read file resources on FTP     Public Static void test02() throws ioexception{URL url=NewURL ("Ftp://wbs14061:[email protected]/information/exe2.txt"); System. out. println ("agreement:"+url.getprotocol ()); System. out. println ("Host:"+url.gethost ()); System. out. println ("Port:"+url.getport ()); System. out. println ("File path:"+url.getpath ()); System. out. println ("file name:"+url.getfile ()); System. out. println ("relative path:"+url.getref ()); System. out. println ("Query:"+url.getquery ()); InputStream is= Url.openstream ();//Gets the input stream of the resource represented by the URL objectBufferedReader br =NewBufferedReader (NewInputStreamReader ( is));//Convert byte input stream to character input streamString str = br.readline (); while(str! =NULL) {System. out. println (str);        str = Br.readline (); } br.close (); is. Close (); }}

Java Learning Note (58)-inetaddress class and URL

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.