2014-11-14 64 views
0
String line = ""; 
List<String> fruit = new LinkedList<String>(); 

try { 
    br = new BufferedReader(new FileReader(filePath)); 
    try { 
     while((line = br.readLine()) != null) 
     { 

      //System.out.println(line); 
      String[] words = line.split(" "); 

      for (String word : words) { 
       if (word.equals("fruit")) { 
       line = br.readLine(); 
       fruit.add(line); 

       } 

      } 


     } 
     br.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

String[] fruitArray = fruit.toArray(new String[0]); 
for (String s : fruitArray) { 
    System.out.println(s); 
    } 
} 
} 

我的代碼存儲線陣列店詞代替線

但我的問題是我需要存儲在陣列由AA空格分隔 我的文字像這樣的話:

水果蘋果木瓜櫻桃獼猴桃

香蕉

我的結果是:

香蕉

謝謝你們

回答

1

更改行:

String[] words = line.split(" "); 

通過

String[] words = line.split("\\s+"); 
+0

我嘗試,但不工作 – 2014-11-15 13:28:58