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

Line Number Reader 

The following example shows how to use LineNumberReader.

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

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

public class LineNumberReaderTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    LineNumberReader reader = null;
    
    try {
      reader = new LineNumberReader(new FileReader("C:\\abc.txt"));
      String line = null;
      
      while ((line = reader.readLine()) != null) {
        System.out.println("Line#" + reader.getLineNumber() ": " + line);  
      }
      
      reader.close();
      
    catch (FileNotFoundException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    }
  }

}
   

It gives the following output,
Line#1: Hello World 
Line#2: WriteTextFile Example
Line#3: Hello World2
Line#4: Hello World3



 
  


  
bl  br