In this Post we will see how to Read Total Number of Lines in File Using JAVA Application
In java by using LineNumberReader class we can get the total number of lines in a file by looping theLineNumberReader.readLine() method.Let's go for code.
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
JAVA CODE :
- package com.system;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.LineNumberReader;
- public class Test
- {
- public static void main(String[] args)
- {
- try{
- File file =new File("D:\\file1234.txt");
- if(file.exists()){
- FileReader fr = new FileReader(file);
- LineNumberReader lnr = new LineNumberReader(fr);
- int lineNum = 0;
- while (lnr.readLine() != null){
- lineNum++;
- }
- System.out.println("Total number of lines in the file : " + lineNum);
- lnr.close();
- }else{
- System.out.println("File does not exists!");
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }

0 comments:
Post a Comment