- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to store the contents of an URL in java?
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>…………………………………………………………………………………………. ………………………………………………………………………………………………… ………………………………………………………………………………………………
Advertisements