문제

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);
        }
도움이 되었습니까?

해결책

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.

다른 팁

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" );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top