2012-08-14 194 views
2
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
String str 
while ((str =in.readLine()) != null) 
{ 
    items = str.split("\n"); 
} 
in.close(); 

字符串(STR)包含像一個文本文件中的數據:如何用'換行'拆分字符串?

每個字是在銀新線。 我想讀取字符串,並將每一個單詞分隔一行並存儲到一個String對象數組中(這將是名爲'items'的變量)。

回答

10

實際上, 已經根據換行符分割輸入。

所以,在這裏你目前有:

items=str.split("\n"); 

你只需要追加str你的陣列。

例如,與infile文件保持:

January 
February 
March 
April 
May 
June 

下面的程序輸出6(創建的數組列表的大小):

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.ArrayList; 
class Test { 
    public static void main (String[] args) { 
     try { 
      ArrayList<String> itms = new ArrayList<String>(); 
      BufferedReader br = new BufferedReader (new FileReader ("infile")); 
      String str; 
      while ((str = br.readLine()) != null) 
       itms.add(str); 
      br.close(); 
      System.out.println (itms.size()); 
     } catch (Exception e) { 
      System.out.println ("Exception: " + e); 
     } 
    } 
} 
+0

感謝此作品! – amg 2012-08-14 06:11:38

2

readLine方法已經讀取線通過-線。此字符串中不會有\n個字符。

試試這個:

ArrayList<String> itemList = new ArrayList<String>(); 
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
String str; 
while ((str = in.readLine()) != null) { 
    itemList.add(str); 
} 
in.close(); 
+0

謝謝你的回覆!!它的作品! – amg 2012-08-14 06:10:55

0

使用Arraylist這個..

ArrayList<String> items= new ArrayList<String>(); 

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
String str; 
while ((str =in.readLine()) != null) 
{ 
    items.add(str.split("\n")); 
} 
in.close(); 

=====>檢查

for(int i=0;i<items.size;i++) 
{ 
    System.out.println("item name "+items.get(i)); 
} 
1

這是我的代碼和它的工作對我來說

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 
     for (String url : urls) { 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      ArrayList<String> itemList = new ArrayList<String>(); 
      try { 
       HttpResponse execute = client.execute(httpGet); 
       InputStream content = execute.getEntity().getContent(); 

       BufferedReader buffer = new BufferedReader(
         new InputStreamReader(content, "iso-8859-1")); 
       StringBuilder sb = new StringBuilder(); 

       String s = null; 

       while ((s = buffer.readLine()) != null) { 

        itemList.add(s); 


       } 

       response = itemList.get(0); 
       content.close(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     textView.setText(Html.fromHtml(result)); 
    } 
} 

public void readWebpage(View view) { 
    DownloadWebPageTask task = new DownloadWebPageTask(); 
    task.execute(new String[] { "http://www.yourURL.com" }); 

} 

readWebpage功能的onclick功能我在按鈕XML創建這樣

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button> 
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView> 

在我的代碼

我試圖讓第一線所以我用itemList.get(0)如果你想得到另一條線只是改變索引itemList.get(1)itemList.get(n)