Java - URL openStream() Method with Examples
Description
The Java URL openStream() method opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for following −
openConnection().getInputStream()
Declaration
Following is the declaration for java.net.URL.openStream() method
public final InputStream openStream()
Parameters
NA
Return Value
an input stream for reading from the URL connection.
Exception
IOException − if an I/O exception occurs.
Example 1
The following example shows the usage of Java URL openStream() method to read content of a website for a valid url with https protocol. In this example, we're creating an instance of URL class. As next, we're using URL.openStream() to get the content of the page of the website pointed by the url and printing the contents of the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>
Example 2
The following example shows the usage of Java URL openConnection() method for a valid url. In this example, we're creating an instance of URL class with http protocol. As next, we're using URL.openStream() to get the content of the page of the website pointed by the url and printing the contents of the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("http","www.tutorialspoint.com","/index.htm");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>
Example 3
The following example shows the usage of Java URL openConnection() method for a valid url with file protocol. In this example, we're creating an instance of URL class. As next, we're using URL.openStream() to get the content of the page of the website pointed by the url and printing the contents of the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("file","www.tutorialspoint.com","/index.htm");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) ....