How do I use LineNumberReader class to read file?

Category: java.io, viewed: 10425 time(s).
package org.kodejava.example.io;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;

public class LineNumberReaderExample
{
    public static void main(String[] args) throws Exception
    {
        // In this example we use LineNumberReader class to read file contents.
        // try to do here is to get the line number of the read data. Instead
        // of introducing another variable; an integer for instance; to keep
        // track the line number we can utilize the LineNumberReader class. 
        // This class offers the getLineNumber() method the know the current
        // line of the data that is read.
        
        File file = null;
        FileReader fr = null;
        LineNumberReader lnr = null;

        try
        {
            // We'll read a file called student.csv that contains our student
            // information data.
            file = new File("student.csv");
            
            // To create the FileReader we can pass in our student data file
            // to the reader. Next we pass the reader into our LineNumberReader
            // class.
            fr = new FileReader(file);            
            lnr = new LineNumberReader(fr);
            
            // If we set the line number of the LineNumberReader here we'll
            // got the line number start from the defined line number + 1
            
            //lnr.setLineNumber(400);

            String line = "";            
            while ((line = lnr.readLine()) != null)
            {
                // We print out the student data and show what line is currently
                // read by our program.
                System.out.println("Line Number " + lnr.getLineNumber() + ": " + line);                
            }
        } finally
        {
            // Don't forget to close the stream when we finish reading the file.
            if (fr != null)
            {
                fr.close();
            }
            if (lnr != null)
            {
                lnr.close();
            }
        }

    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats