2014-11-14 62 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