<< Chapter < Page Chapter >> Page >

Complete program listings

Listing 8 - The program named Java4640a.

/*File Java4640a.java Copyright 1998, R.G.Baldwin Revised 01/05/14 This program exercises four of the constructors andsix of the methods of the URL class. The program also illustrates the use of the URLEncoderclass to convert a string containing spaces and other such characters into UTF-8 format.**********************************************************/ import java.net.*;import java.io.*; class Java4640a{public static void main(String[] args){Java4640a obj = new Java4640a(); try{System.out.println( "Use simple string constructor for host URL");obj.display(new URL("http://www.austincc.edu"));System.out.println("Use simple string constructor " + "for host plus file");obj.display(new URL( "http://www.austincc.edu/baldwin"));System.out.println("Use strings for protocol, host, and file"); obj.display(new URL("http","www.austincc.edu","/baldwin"));System.out.println("Use strings for protocol " + "host, and file\n and int for port");obj.display(new URL( "http","www.austincc.edu",80,"/baldwin"));System.out.println("Construct absolute URL from " +"host URL and relative URL"); URL baseURL = new URL("http://www.austincc.edu/baldwin/hello.html"); obj.display(new URL(baseURL,"/baldwin/Index.html"));System.out.println("Now use URLEncoder to create " +"UTF-8 encoded String"); System.out.println("http://space .tilde~.plus+.com");System.out.println(URLEncoder.encode( "http://space .tilde~.plus+.com","UTF-8"));}catch(MalformedURLException | UnsupportedEncodingException e){e.printStackTrace(); }//end catch}//end main //-----------------------------------------------------//void display(URL url){//method to display parts of URLSystem.out.print(url.getProtocol() + " "); System.out.print(url.getHost() + " ");System.out.print(url.getPort() + " "); System.out.print(url.getFile() + " ");System.out.println(url.getRef());//Now display entire URL as a string. System.out.println(url.toString());System.out.println(); }//end display}//end class Java4640a
Listing 9 - The program named Java4640d.
/*File Java4640d.java Copyright 1998, R.G.Baldwin Revised 01/06/14 Illustrates connecting to a URL and reading a file fromthat URL as an input stream. Computer must be online for this program to run properly.Otherwise, it will throw an exception of type UnknownHostException.**********************************************************/ import java.net.*;import java.io.*; class Java4640d{public static void main(String[] args){String dataLine; try{//Get a URL object URL url = new URL("http://www.austincc.edu/baldwin/page1.html");//Open a connection to this URL and return an // input stream for reading from the connection.BufferedReader htmlPage = new BufferedReader(new InputStreamReader(url.openStream()));//Read and display file one line at a time. while((dataLine = htmlPage.readLine()) != null){System.out.println(dataLine); }//end while loop}//end try catch(Exception e){e.printStackTrace(); }//end catch}//end main }//end class Java4640d

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask