0

Web サイト (URL: http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698 ) からすべてのテキストを取得し、複数行ではなく単一行の文字列に入れるにはどうすればよいですか? 、現在、約20行のギブまたはテイクがありますが、1行にしたいです。

テキストを取得するためのコード:

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

行ごとにではなく出力を取得します。つまり、while ((line = br.readLine()) != null) 使用からwhile (int c != -1)すべての文字を読み取り、stringbuilder に入れます。次に、最後に文字列を出力します。

編集:以下のコードを使用すると、機能します:

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

実行すると、出力が 1 行であることがわかると思います。String st = new String(data)持っています。

于 2013-08-21T21:04:54.860 に答える