Friday, September 16, 2011

JAVA FileLineCount

How many lines are in a text File  

        public static int FileLineCount(String FileName)
        {
         /*
         * (c) John Minkjan
         * 101Java.blogspot.com
         * 2011 till 2031
         */
    int rCount = 0;   
            try{
            File f = new File(FileName);
            if (f.exists()){
                FileReader fr = new FileReader(f);
                LineNumberReader ln = new LineNumberReader(fr);
                int count = 0;
                while (ln.readLine() != null){
                    count++;
                }
                //System.out.println("Total line no: " + count);
                ln.close();
                                rCount = count;
            }
            else{
                System.out.println("File does not exists!");
            }
                       
                       
        }
        catch(IOException e){
            e.printStackTrace();
        }
             return rCount;  
    }

Till Next Time

No comments:

Post a Comment