Question

I have this, but I was wondering if there is a faster way:

        URL url=new URL(page);
        InputStream is = new BufferedInputStream(url.openConnection().getInputStream());
        BufferedReader in=new BufferedReader(new InputStreamReader(is));
        String tmp="";
        StringBuilder sb=new StringBuilder();
        while((tmp=in.readLine())!=null){
            sb.append(tmp);
        }
Was it helpful?

Solution

Probably network is the biggest overhead, there isn't much you can do on Java code side. But using IOUtils is at least much faster to implement:

String page = IOUtils.toString(url.openConnection().getInputStream());

Remember to close underlying stream.

OTHER TIPS

if you need manipulating with your html, find some library. Like for example jsoup.

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

Example:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Elements newsHeadlines = doc.select("#mp-itn b a");

If you're using Apache Commons IO's IOUtils as Tomasz suggests, there's an even simpler method: toString(URL), or its preferred cousins that take a charset (of course that requires knowing the resource's charset in advance).

String string = IOUtils.toString( new URL( "http://some.url" ));

or

String string = IOUtils.toString( new URL( "http://some.url" ), "US-ASCII" );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top