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);
}
}
}
我的代码将行存储在数组中
但我的问题是我需要在数组中存储一个单词,并用空格分隔 我的文字是这样的:
fruit apples papaya cherry kiwi
banana
我的结果是:
banana
谢谢大家
最佳答案
更改行:
String[] words = line.split(" ");
由
String[] words = line.split("\\s+");
关于java - 存储单词而不是行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930446/