How to read webpage / URL using java code?

UPDATED: 15 September 2011
Some of you think how facebook shows the webpage. It actually retrieve the content of the page. how to read webpage in java code/Program?

Some time we need to retrieve the data of web page when there is data in XML format. You can also read the page using java here I'm putting sample code that will help you to read the page line by line.

public class readURL  {
    public static void main(String[] args) {
        String generate_URL = "";
        String inputLine;
        try {
            URL data = new URL(generate_URL);
             * Proxy code start 
             * If you are working behind firewall uncomment below lines. 
             * Set your proxy server

            /* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 8080)); */
            /* HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy); */
            /* Proxy code end */
            /* Open connection */
            /* comment below line in case of Proxy */
            HttpURLConnection con = (HttpURLConnection) data.openConnection(); 
            /* Read webpage coontent */
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            /* Read line by line */
            while ((inputLine = in.readLine()) != null) {
            /* close BufferedReader */
            /* close HttpURLConnection */
        } catch (Exception e) {
Run the above code that will give you source code of that page. Now modify the above code as per requirement. It'll read the data in all the type. XML,HTML,etc...

Use of Proxy
You need to take care for proxy connection and it depends upon whether you are writing program for applet or desktop application. If you are writing code for applet then in production applet, you don't need proxy connection as server will have all access and will work without proxy. If you are working on desktop application that run within the Local Area Network then you must set proxy in case of firewall.