1. 扫描当前(而不是下一条)行位置。 2.扫描X行(Java初学者)
假设我有一个文本文件,我正在从...输入文本,
File file = new File("example.txt");
Scanner inputFile = new Scanner(file);
如果我想引用下一行文本,我会这样做
inputfile.nextLine();
假设我想再次引用同一行文本。有类似“currentLine()”的方法吗?我还能做什么?
一般来说,假设我想打开文件并引用文本的第 3 行或第 150 行或其他内容,如何让扫描仪读取该特定行?
一般来说
let's say I have a text file I'm inputing text from...
File file = new File("example.txt");
Scanner inputFile = new Scanner(file);
if I want to reference the next line of text I would do
inputfile.nextLine();
Let's say I want to reference that same line of text again. Is there like a "currentLine()" method? What else could I do?
In general, let's say I want to open the file and refer to the 3rd line of text or the 150th line or whatever, how do I get the Scanner to read that specific line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有 currentLine() 方法。您可以将当前行存储在临时变量
String currLine = inputfile.nextLine();
中,或者您可以创建自己的方法。你可以这样做:
There is no currentLine() method. You can store current line in the temp var
String currLine = inputfile.nextLine();
or you can create your own method.You can do it this way:
仅当您想逐行处理文件时,扫描仪才有用。
如果您愿意,您可以将每一行存储在集合中以供将来参考。
或者,您可以使用 Commons IO,例如检索特定行:
http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html#readLines(java.io.File)< /a>
并按照您建议的方式引用该行的每一行。在我们知道您想做什么之前,很难提出其他建议。
Scanner is only good if you want to process a file line by line.
You could store each line in a collection for future reference, if you wanted to.
Alternatively you could use Commons IO, e.g. to retrieve a specific line:
http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html#readLines(java.io.File)
and refer to each line of the line in the manner you suggest. It's hard to suggest anything else until we know what you're trying to do.