Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Reading a webpage in Java (See related posts)

A trivial piece of example code demonstrating how to get a BufferedReader from a Url as a String and do something with it. This code simply prints the contents of the website at the first argument to stdout.

   1  
   2  import java.io.*;
   3  import java.net.URL;
   4  
   5  public class WebsiteReader
   6  {
   7  	public static BufferedReader read(String url) throws Exception{
   8  		return new BufferedReader(
   9  			new InputStreamReader(
  10  				new URL(url).openStream()));}
  11  
  12  	public static void main (String[] args) throws Exception{
  13  		BufferedReader reader = read(args[0]);
  14  		String line = reader.readLine();
  15  
  16  		while (line != null) {
  17  			System.out.println(line);
  18  			line = reader.readLine(); }}
  19  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5555 code snippets

Related Posts