1. 扫描当前(而不是下一条)行位置。 2.扫描X行(Java初学者)

发布于 2024-09-28 16:33:41 字数 354 浏览 0 评论 0原文

假设我有一个文本文件,我正在从...输入文本,

File file = new File("example.txt");

Scanner inputFile = new Scanner(file);

如果我想引用下一行文本,我会这样做

inputfile.nextLine();
  1. 假设我想再次引用同一行文本。有类似“currentLine()”的方法吗?我还能做什么?

  2. 一般来说,假设我想打开文件并引用文本的第 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();
  1. Let's say I want to reference that same line of text again. Is there like a "currentLine()" method? What else could I do?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2024-10-05 16:33:41
  1. 没有 currentLine() 方法。您可以将当前行存储在临时变量 String currLine = inputfile.nextLine(); 中,或者您可以创建自己的方法。

  2. 你可以这样做:


public static void main(String[] args) throws FileNotFoundException{
  Scanner inputFile = new Scanner(new File("example.txt"));
  System.out.println(getLine(150, inputFile));
}
public static String getLine(int line, Scanner input){
  String result = "";
  int lineNr = 1;
  while(input.hasNextLine() && lineNr <= line){
    result = input.nextLine();
    lineNr++;
  }
  return result;
}
  1. 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.

  2. You can do it this way:


public static void main(String[] args) throws FileNotFoundException{
  Scanner inputFile = new Scanner(new File("example.txt"));
  System.out.println(getLine(150, inputFile));
}
public static String getLine(int line, Scanner input){
  String result = "";
  int lineNr = 1;
  while(input.hasNextLine() && lineNr <= line){
    result = input.nextLine();
    lineNr++;
  }
  return result;
}
鹿港巷口少年归 2024-10-05 16:33:41

仅当您想逐行处理文件时,扫描仪才有用。

如果您愿意,您可以将每一行存储在集合中以供将来参考。

或者,您可以使用 Commons IO,例如检索特定行:

List<String> lines = FileUtils.readLines(file);
lines.get(150);

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:

List<String> lines = FileUtils.readLines(file);
lines.get(150);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文