/* 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
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:
Post a Comment