【发布时间】:2016-12-06 21:05:51
【问题描述】:
在 Java 中,我使用 Scanner 从文本文件中读取,
例如(猫、狗、老鼠)。
当我使用System.out.println() 时,输出看起来像cat, dog, mouse
我希望列表看起来像这样
cat
dog
mouse
下面的任何帮助代码
Scanner scan = null;
Scanner scan2 = null;
boolean same = true;
try {
scan = new Scanner(new
File("//home//mearts//keywords.txt"));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
List<String> firstLines = new ArrayList<String>();
while (scan.hasNextLine()) {
firstLines.add(scan.nextLine());
System.out.println(firstLines);
}
【问题讨论】:
-
你不能使用 for 循环吗?
标签: java list file java.util.scanner