0

我如何从网站(网址: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
        }
    }
}

编辑:你不必给我所有的代码,只要指向正确的方向。:)

4

2 回答 2

1

简单,System.out.println改成System.out.print. 完毕。:-D

要返回 a相反,只需在循环外String创建一个,然后将每一行输入到它。StringBuilderappend


演示后者的示例代码(我刚刚意识到 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();
于 2013-08-21T20:58:04.477 回答
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)拿着它。

于 2013-08-21T21:04:54.860 回答