如何从Java中的BufferedReader对象提取全部内容的最佳方法? [英] How is the best way to extract the entire content from a BufferedReader object in Java?

查看:637
本文介绍了如何从Java中的BufferedReader对象提取全部内容的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过URLConnection获取整个网页。

i'm trying to get an entire WebPage through a URLConnection.

最有效的方法是什么?

我已经在这样做:

URL url = new URL("http://www.google.com/");
URLConnection connection;
connection = url.openConnection();
InputStream in = connection.getInputStream();        
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
StringBuffer html = new StringBuffer();
String line = bf.readLine();
while(line!=null){
    html.append(line);
    line = bf.readLine();
}
bf.close();

html拥有整个HTML页面。

html has the entire HTML page.

推荐答案

您的方法看起来不错,但是您可以通过避免为每行创建中间String对象来使它更有效。

Your approach looks pretty good, however you can make it somewhat more efficient by avoiding the creation of intermediate String objects for each line.

执行此操作的方法是直接读入临时char []缓冲区。

The way to do this is to read directly into a temporary char[] buffer.

此处是执行此操作的代码的略微修改版本(减去所有错误检查,异常处理等。)

Here is a slightly modified version of your code that does this (minus all the error checking, exception handling etc. for clarity):

        URL url = new URL("http://www.google.com/");
        URLConnection connection;
        connection = url.openConnection();
        InputStream in = connection.getInputStream();        
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        StringBuffer html = new StringBuffer();

        char[] charBuffer = new char[4096];
        int count=0;

        do {
            count=bf.read(charBuffer, 0, 4096);
            if (count>=0) html.append(charBuffer,0,count);
        } while (count>0);
        bf.close();

为了获得更高的性能,您当然可以做一些额外的事情,例如预分配字符数组和如果需要频繁调用此代码,则为StringBuffer。

For even more performance, you can of course do little extra things like pre-allocating the character array and StringBuffer if this code is going to be called frequently.

这篇关于如何从Java中的BufferedReader对象提取全部内容的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆