从网站获取文本并将其放入一行?

时间:2013-08-21 20:57:11

标签: java bufferedreader

如何从网站上获取所有文本(网址:http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698)并将其放入单行字符串中,而不是多行,目前它将有大约20行给予或接受,但是我希望它在一条线上。

检索文字的代码:

public static void getTextFromURL() {
    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;

    try {
        url = new URL("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698");
        is = url.openStream();    // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }
}

编辑:你不必给我所有的代码,只是一个正确方向的点。 :)

2 个答案:

答案 0 :(得分:1)

很简单,将System.out.println更改为System.out.print。完成。 :-D

要返回String,只需在循环外创建StringBuilder,然后在其输入的每一行append


演示后者的示例代码(我刚刚意识到OP想要什么,这是空格而不是换行符):

StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
    if (result.length() != 0)
        result.append(' ');
    result.append(line);
}
return result.toString();

Hiren Patel阅读每个角色的风格也很有效:

StringBuilder result = new StringBuilder();
while ((c = br.read()) != -1)
    result.append(c == '\n' ? ' ' : (char) c);
return result.toString();

答案 1 :(得分:1)

检索输出而不是逐行检索, 即从while ((line = br.readLine()) != null)使用while (int c != -1)读取所有字符并将它们放入字符串构建器中。 然后在最后打印字符串。

编辑: 使用下面的代码,它的工作原理:

public static void main(String args[]) {
        URL url;
        InputStream is = null;
        try {
            url = new URL("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698");
            is = url.openStream(); // throws an IOException            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch = is.read();
            while (ch != -1) 
            {
                if((char)ch != '\n')                
                    baos.write(ch);
                ch = is.read();
            }
            byte[] data = baos.toByteArray();
            String st = new String(data);
            System.out.println(st);
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }
    }

输出是: 501844,110,34581332115,30,14114403982,1,18325274,31,15814460203,14,2405287276,11,1366419761,1,67679445,1,0505401,1,70522524,1,75454208,1,0505244,1,20505816,1,40469998,1,0337042,2,155393308,5,437403072,1,0488016,1,0524106,1,0428961,1,0389021,1,0382198,1,0383592,1,0362267,1,0-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1

希望你跑步时,你会明白输出是一行的。 String st = new String(data)持有它。

相关问题