URI_URL2
Reading html file  directly from a URI or URL with openStream() method
  • URI uri = new URI(str1);
  • URL aURL = uri.toURL();
  • URLConnection ucon = aURL.openConnection();
  • BufferedReader in = new BufferedReader(
    new InputStreamReader(aURL.openStream()));
  • while ((inputLine = in.readLine()) != null){
    System.out.println(inputLine);}
Reading directly from a URL

Complete Code:

/**
* This is Template 1 workplace
*/
import java.net.*;
import java.util.Date;
import java.io.*;


public class ClassTemplate1 {
/**
*
*/
public ClassTemplate1() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
String str = "";
System.out.print("Type URL: ");
str = br.readLine();
process_this pt = new process_this();
pt.method_parse_url(str);
}

}

class process_this
{
public void method_parse_url(String str)throws IOException
{
try {
String str1 = str; String inputLine;
// could not use MalformedURLException
URI uri = new URI(str1);URL aURL = uri.toURL();
URLConnection ucon = aURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(aURL.openStream()));
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);}
in.close();
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("Connection type = " + ucon.getContentType());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
} catch (MalformedURLException | URISyntaxException e) {
// TODO Auto-generated catch block
//URISyntaxException or MalformedURLException
e.printStackTrace();
}
//URL aURL = new URL(str);

}
}
 

Runtime Views: