【问题标题】:Get line number from a file从文件中获取行号
【发布时间】:2013-07-14 03:02:55
【问题描述】:

如何实现一种方法来返回当前正在从文件中扫描的行的行号。我有两台扫描仪,一台用于文件(fileScanner),另一台用于线路(lineScanner)

这是我有的,但我不知道我是否需要构造函数中的行号!

public TextFileScanner(String fileName) throws FileNotFoundException
{
    this.fileScanner = new Scanner(new File(fileName));
    this.lineScanner = new Scanner(this.fileScanner.nextLine());
    this.lineNumber = 1;
}

我需要这个方法:

public int getLineNumber()
{

}

【问题讨论】:

  • 你不妨把它传递给构造函数,并使用访问器来检索它。
  • 谢谢,在实现 getLineNumber() 时遇到问题,我刚开始使用 java,但我正在努力。
  • 添加C++或Java等语言标签,让更多人看到。

标签: java line-numbers


【解决方案1】:

您可以只使用一个Scanner 对象来读取文件并报告行号。

这是一个示例代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LineNumber {

    public static void main(String [] args) throws FileNotFoundException {

        System.out.printf("Test!\n");

        File f = new File("test.txt");
        Scanner fileScanner = new Scanner(f);

        int lineNumber = 0;
        while(fileScanner.hasNextLine()){
            System.out.println(fileScanner.nextLine());
            lineNumber++;
        }

        fileScanner.close();
        System.out.printf("%d lines\n", lineNumber);

    }
}

现在,如果您想使用面向对象的编程方法执行此操作,那么您可以执行以下操作:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileProcessor {

    // Mark these field as private so the object won't get tainted from outside
    private String fileName;
    private File file;

    /**
     * Instantiates an object from the FileProcessor class
     * 
     * @param fileName
     */
    public FileProcessor(String fileName) {
        this.fileName = fileName;
        this.file = new File(fileName);
    }

    public int getLineNumbers() {

        Scanner fileScanner = null;

        try {
            fileScanner = new Scanner(this.file);
        } catch (FileNotFoundException e) {
            System.out.printf("The file %s could not be found.\n",
                    this.file.getName());
        }

        int lines = 0;

        while (fileScanner.hasNextLine()) {
            lines++;
            // Go to next line in file
            fileScanner.nextLine();
        }

        fileScanner.close();

        return lines;
    }

    /**
     * Test our FileProcessor Class
     * 
     * @param args
     * @throws FileNotFoundException
     */

    public static void main(String[] args) throws FileNotFoundException {

        FileProcessor fileProcessor = new FileProcessor("text.txt");
        System.out.printf("%d lines\n", fileProcessor.getLineNumbers());
    }
}

【讨论】:

  • 谢谢,我知道了,我在我的程序上进行了一些调整,效果很好,非常感谢。
  • 很高兴为您提供帮助。如果有帮助,请接受它作为答案,或者添加您自己的答案并将其标记为答案。
【解决方案2】:

打印当前行号:

System.out.println("行号为" + new Exception().getStackTrace()[0].getLineNumber());

例子:

公共类 LineNumberTest{

 public static void main(String []args){
    System.out.println("The line number is " + new Exception().getStackTrace()[0].getLineNumber());
 }

}

【讨论】:

  • 返回源代码中的当前行号
猜你喜欢
  • 2020-09-21
  • 2015-09-29
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多