我需要将用户输入文本文件的每一行(单个单词)的内容添加到数组中的单独元素中。*我知道 ArrayList 是解决此问题的更好数据结构,但我仅限于使用数组。
这是我的代码:
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a file name: ");
System.out.flush();
String filename = scanner.nextLine();
File file = new File(filename);
FileReader reader = new FileReader(file);
try (BufferedReader buffReader = new BufferedReader(reader)) {
String line;
int i=0;
String[] words = new String[10];
while((line = buffReader.readLine()) != null) {
words[i]=buffReader.readLine();
System.out.println(words[i]);
i++;
}
}
catch(IOException e){
System.out.println(e);
}
}
输入文件很简单:
Pans
Pots
opt
sit
it's
snap
程序输出如下。它似乎正在跳过每隔一行。
Pots
sit
snap