Search This Blog

Saturday, 1 February 2014

get the total number of lines in a text file

/* count total number of lines in a file  */


import java.io.*;;

public class get_length
{
    public static void main(String[] args)
    {
        File f=new File("E://NewFile.txt");
        try
        {
           FileReader fr=new FileReader(f);
           LineNumberReader lr=new LineNumberReader(fr);
           int count=0;
           while(lr.readLine()!=null)
           {
               count++;
           }
           System.out.println("Total lines are : "+count);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

Output

Total lines are : 6

File E://NewFile.txt  is


No comments:

Popular Posts