Crawle web page - Java java.net

Java examples for java.net:URL Read

Description

Crawle web page

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Main {
    /**/*  w  w  w.j a  v  a 2s.  c  om*/
     * Crawle webpage
     * 
     * @param url
     * @return
     * @throws IOException 
     */
    public static StringBuffer crawle(URL url) throws IOException {
        StringBuffer page = new StringBuffer();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String str;
        while ((str = in.readLine()) != null) {
            page.append(str);
        }
        in.close();

        return page;
    }

    /**
     * Crawle webpage
     * @param urlStr
     * @return
     * @throws IOException
     */
    public static StringBuffer crawle(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        return crawle(url);
    }
}

Related Tutorials