Get line number from a file

18,620

You can use just one Scanner object to read a file and reports the line numbers.

Here is a sample code:

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);

    }
}

Now, if you want do this using an Object-Oriented Programming approach then you can do something like this:

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());
    }
}
Share:
18,620
user2580264
Author by

user2580264

Updated on June 13, 2022

Comments

  • user2580264
    user2580264 almost 2 years

    How can I implement a method to return the line-number of the line currently being scanned from a file. I have two scanners, one for the file (fileScanner) and another for the line (lineScanner)

    this is what I have, but I don't know if I need the linenumber in the constructor!

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

    and I need this method:

    public int getLineNumber()
    {
    
    }
    
  • user2580264
    user2580264 almost 11 years
    Thanks I got it, I tried on my program with few adjusts, and it worked perfectly, I really appreciate it.
  • hyde
    hyde almost 11 years
    Glad to help. Please accept it as answer if it was helpful or add your own answer and mark it as answer.
  • Felix
    Felix over 4 years
    This returns the current line number in your sourcecode