How to store the contents of an URL in java?

Java 8Object Oriented ProgrammingProgramming

The openStream() method of the URL class opens a connection and returns an InputStream. Using this stream, you can you read contents from that connection.

Example

import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class ReadingURL {
   public static void main(String args[]) throws Exception {
      URL url = new URL("http://www.tutorialspoint.com/");
      InputStream inputStream = url.openStream();
      Scanner s = new Scanner(inputStream);
      while(s.hasNext()) {
         System.out.println(s.nextLine());
      }
   }
}

Output

<li><a href="/couchdb/index.htm" title="Learn CouchDB">Learn CouchDB</a></li>
<li><a href="/db2/index.htm" title="Learn DB2">Learn DB2</a></li>
<li><a href="/documentdb_sql/index.htm" title="Learn DocumentDB SQL">
   Learn DocumentDB SQL</a></li>
<li><a href="/documentdb/index.htm" title="Learn DocumentDB">Learn DocumentDB</a></li>
<li><a href="/dynamodb/index.htm" title="Learn DynamoDB">Learn DynamoDB</a></li>
<li><a href="/h2_database/index.htm" title="Learn H2 Database">
   Learn H2 Database</a></li>
<li><a href="/hsqldb/index.htm" title="Learn HSQLDB">Learn HSQLDB</a></li>
<li><a href="/ims_db/index.htm" title="Learn IMS DB">Learn IMS DB</a></li>
<li>………………………………………………………………………………………….
…………………………………………………………………………………………………
………………………………………………………………………………………………
raja
Published on 09-Jan-2018 09:50:01
Advertisements