tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Coding Examples > Line Number Reader

Line Number Reader 

Print a text file line by line along with line numbers.

File Name  :  
com/bethecoder/tutorials/coding/examples/LineNumberReaderTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.coding.examples;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderTest {

  /**
   @param args
   @throws IOException
   */
  public static void main(String[] argsthrows IOException {
    FileReader freader = null;
    LineNumberReader lnreader = null;
    try {
      freader = new FileReader("C:/abcd.txt");
      lnreader = new LineNumberReader(freader);
      String line = null;
      
      while ((line = lnreader.readLine()) != null) {
        System.out.println("Line#" + lnreader.getLineNumber() ": " + line);
      }
      
    finally {
      freader.close();
      lnreader.close();
    }
  }

}
   

It gives the following output,
Line#1: ABCD
Line#2: ABCD
Line#3: ABCD
Line#4: ABCD



 
  


  
bl  br