URL openStream Example
URL class openStream example. This example shows you how to use openStream method.
URL class openStream example. public final InputStream openStream() throws IOException Opens a connection to this URL and returns an InputStream for reading from that connection.
Here is the code
/*
* @ # OpenStream.java
* A class repersenting use to openStream method
* of URL class in java.net package
* version 24 June 2008
* author Rose India
*/
import java.net.*;
import java.io.*;
public class OpenStream {
public static void main(String args[]) throws Exception {
URL url = new URL("http://localhost:8080/programs/index.html");
BufferedReader br = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
}
} |
Output
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hi!</h2>
</body>
</html> |
|