2011-05-31 54 views
3

在java中,我需要從「http://www.mysite.com/text.txt」獲得哪些代碼才能解析網站中包含的結果文本儘可能少的行數。來自URL的Java單行掃描器文本文件

+0

請定義:'解析' – 2011-05-31 07:24:11

+0

您可以儘可能在幾行內做到這一點,或者您可以用可靠的代碼來做到這一點。不是在同一時間。 – 2011-05-31 07:58:04

回答

5
URL yahoo = new URL("http://www.yahoo.com/"); 
    BufferedReader in = new BufferedReader(
       new InputStreamReader(
       yahoo.openStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
     System.out.println(inputLine); 

    in.close(); 

參考

+0

爲什麼要投票? ,小心添加原因 – 2011-05-31 07:39:39

+0

不知道反對意見是什麼,但我認爲這是最穩健的答案。我已經嘗試了以下兩項,但成功率較低。 作爲一個班輪: '掃描儀掃描儀=新的掃描儀(新的BufferedReader(新的URL(segmentationpath).openStream())));' – 2011-05-31 14:55:10

+0

我怎樣才能把它寫到word doc? – santafebound 2017-05-10 13:39:08

7
Scanner sc = new Scanner(new URL("http://www.mysite.com/text.txt").openStream()); 
+1

使用其中一個需要更多代碼的選項,比如上面看到的接受答案,有沒有什麼好處? – Evorlor 2014-11-01 15:08:29

4

here兩者,沒有測試

URLConnection connection = new URL("http://www.mysite.com/text.txt").openConnection(); 
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();