DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Reading A Webpage In Java

02.20.2007
| 62751 views |
  • submit to reddit
        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. 

import java.io.*;
import java.net.URL;

public class WebsiteReader
{
	public static BufferedReader read(String url) throws Exception{
		return new BufferedReader(
			new InputStreamReader(
				new URL(url).openStream()));}

	public static void main (String[] args) throws Exception{
		BufferedReader reader = read(args[0]);
		String line = reader.readLine();

		while (line != null) {
			System.out.println(line);
			line = reader.readLine(); }}
}
    

Comments

Carla Brian replied on Mon, 2012/07/16 - 6:03pm

I want to learn more on this one. I need more tutorials on how to create webpage using java. I think I am more familiar with PhP. I wonder which of these two is effective. - Mercy Ministries