Devise a sequence to display a count of lines containing str
Devise a sequence to display a count of lines containing strings containing <BR /> in all .html files in the current directory. <BR /> is case-insensitive. It can be upper or lower case.
Solution
import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.*;
/**
 *
 * This Java program count the <br/> tags in all the files present in the current directory
 *
 * @author Surya Sumanth
 */
 public class CountingTags {
    public static void main(String args[]) {
       
   
     try {
       
     //Give your directory path in the below constructor File(\"\")
     File folder = new File(\"C:/YourDirectory\");
   
     //Lists all the files in the given Directory
     File[] listOfFiles = folder.listFiles();
   
     //Initializes <br/> count to zero
     int countofBR = 0;
       for (int i = 0; i < listOfFiles.length; i++) {
      
        //Check Whether the Current file is a File or Directory
        if (listOfFiles[i].isFile()) {
       
       
         FileInputStream fis = null;
         BufferedReader reader = null;
           
             //Count the <br/> tags only in .html files only
           
         if(listOfFiles[i].getName().endsWith(\".html\")){
               
              fis = new FileInputStream(listOfFiles[i].getName());
             reader = new BufferedReader(new InputStreamReader(fis));
        
             //Reading File line by line using BufferedReader
        
             String line = reader.readLine().toLowerCase();
             while(line != null){
               
                  //Check <br/> is a substring in the current line as we converted the line already to lower case.
                 int index1 = line.indexOf(\"<br/>\");
                 if (index1 != -1){
                     countofBR++;
                 }
                 line = reader.readLine();
             }
         }
       
        }else{
           //Repeat the same process above as this is a directory if you want to check deeper.
           //Else Leave this if you need only the <br/> count of the files present in the current root directory only.
        }
        }
        
     } catch (Exception ex) {
             ex.printStackTrace();
     }
     //close the BufferedReader and FileInputStream objects.
     finally {
             try {
                 reader.close();
                 fis.close();
             } catch (Exception ex) {
                 ex.printStackTrace();
             }
      }
 }
 }


