2013-08-21 53 views
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 
     } 
    } 
} 

编辑:你没有给我所有的代码,只是在正确的方向点。 :)

回答

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(); 
+0

哇,我现在觉得愚蠢:)但不是意味着我的字符串将在那一行或只是'印刷'已被使用? –

+0

我不确定我是否理解你的问题。你只是想把行打印到屏幕上,还是你想要返回一个字符串?如果后者,如我的回答所提到的,只需创建一个'StringBuilder'并'追加'你从页面读取的每一行。 –

+0

@WillBaker在这里,我添加了一些示例代码来演示我的想法.... –

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

希望当u运行,你也会明白,输出为单行。 String st = new String(data)拿着它。

+0

Nah,那也包括换行符。 OP想要删除换行符。 –

+0

您知道,当您通过单个字符读取输出时,可以检查读字符是否为'\ n',然后不添加到字符串生成器,否则添加到字符串生成器。这将确保您最后只有一个字符串,并且不会有'\ n'。或者你可以用''(空格)替换'\ n'。当你逐字阅读而不是逐行阅读时,你将拥有更多的权力。希望这会解决你的查询。 –

+0

当然,但你应该在你的答案中说明。 :-) –

相关问题