我有一个文本文件,其中包含以下内容:
ac und
accipio annehmen
ad zu
adeo hinzugehen
...
我读取文本文件并遍历各行:
Scanner sc = new Scanner(new File("translate.txt"));
while(sc.hasNext()){
String line = sc.nextLine();
}
每行有两个词。 java中有没有任何方法可以获取下一个单词,或者我是否必须拆分行字符串才能获取单词?
您不必分割该行,因为 java.util.Scanner 的默认分隔符是空格。
您可以在 while 语句中创建一个新的 Scanner 对象。
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("translate.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.next();
System.out.println(s);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)