简体   繁体   English

Java 扫描仪,如何逐行读取文本文件而不总是转到下一行

[英]Java scanner, how to read a text file line by line without always going to the next line

If I use the nextline() method in the scanner object, then it will take in that line and then go to the next one.如果我在扫描仪 object 中使用 nextline() 方法,那么它将进入该行,然后将 go 放入下一行。 Is there a way to read in the line and not go to the next line?有没有办法在行中读取而不是go 到下一行? Is there a different class to use that will read in a text file line by line and only go to the next line when told to, as opposed to automatically jumping to the next line once it has read in the current line?是否有不同的 class 可以使用,它将逐行读取文本文件,并且在被告知时仅 go 到下一行,而不是在读取当前行后自动跳转到下一行?

as commented by Goion, you can change the delimiter (eg useDelimiter("\\n") ) and use next() , which does not consume the delimiter (line separator).正如 Goion 所评论的那样,您可以更改分隔符(例如useDelimiter("\\n") )并使用next() ,它不会消耗分隔符(行分隔符)。

Not sure what that is good for;不知道这有什么好处; be aware that most console input are line-oriented , that is, input will only be processed after user types the enter or return key - Scanner will block until that line is available.请注意,大多数控制台输入都是面向行的,也就是说,只有在用户键入 enter 或 return 键后才会处理输入 - Scanner将阻塞直到该行可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM