用java读取下一个单词
我有一个包含以下内容的文本文件:
ac und
accipio annehmen
ad zu
adeo hinzugehen
...
我读取该文本文件并迭代各行:
Scanner sc = new Scanner(new File("translate.txt"));
while(sc.hasNext()){
String line = sc.nextLine();
}
每行有两个单词。 java中有没有任何方法可以获取下一个单词,或者我是否必须拆分行字符串才能获取单词?
I have a text file that has following content:
ac und
accipio annehmen
ad zu
adeo hinzugehen
...
I read the text file and iterate through the lines:
Scanner sc = new Scanner(new File("translate.txt"));
while(sc.hasNext()){
String line = sc.nextLine();
}
Each line has two words. Is there any method in java to get the next word or do I have to split the line string to get the words?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不必分割该行,因为 java.util.Scanner 的默认分隔符是空格。
您可以在 while 语句中创建一个新的 Scanner 对象。
You do not necessarily have to split the line because java.util.Scanner's default delimiter is whitespace.
You can just create a new Scanner object within your while statement.
您已经在这行代码中得到了下一行:
要获取一行中的单词,我建议使用:
You already get the next line in this line of your code:
To get the words of a line, I would recommend to use:
使用扫描仪,您最终将为每一行生成大量对象。对于大文件,GC 会产生相当多的垃圾。而且,它比使用 split() 慢了近三倍。
另一方面,如果按空格分割 (
line.split(" ")
),则当您尝试读取具有不同空格分隔符的文件时,代码将会失败。如果split()
希望您编写一个正则表达式,并且无论如何它都会匹配,请使用split("\\s")
来代替,这样可以多匹配“位”空白不仅仅是一个空格字符。PS:抱歉,我无权对已经给出的答案发表评论。
Using
Scanner
s, you will end up spawning a lot of objects for every line. You will generate a decent amount of garbage for the GC with large files. Also, it is nearly three times slower than using split().On the other hand, If you split by space (
line.split(" ")
), the code will fail if you try to read a file with a different whitespace delimiter. Ifsplit()
expects you to write a regular expression, and it does matching anyway, usesplit("\\s")
instead, that matches a "bit" more whitespace than just a space character.P.S.: Sorry, I don't have right to comment on already given answers.
你最好读一行然后进行拆分。
you're better off reading a line and then doing a split.
您可以使用 Scanner 逐字读取, Scanner.next() 读取下一个字
You can just use Scanner to read word by word, Scanner.next() reads the next word