This 0ne Pr0grammer
This 0ne Pr0grammer

Reputation: 2662

Count the number of lines from point A to point B in Text File

I am trying to figure out how to find the first instance of a string in a given text file, start counting the number of lines starting from the first line with that instance of the string, then stop counting when another/different string is found in a new line.

Basically I have a text file like:

CREATE PROCEDURE "dba".check_diff(a smallint,
                             b       int, 
                             c       int,
                             d  smallint,
                             e smallint,
                             f   smallint,
                             g   smallint,
                             h     smallint,
                             i    smallint)

RETURNING int;
DEFINE p_ret int;
LET p_ret = 0;

END IF;

RETURN p_ret;

END PROCEDURE;

And I just want to find the first instance of "CREATE PROCEDURE" and increment a variable until I reach the instance of "END PROCEDURE;", then reset the variable.

What I have been trying is...

import java.io.*;

public class countFile 
{
    public static void main(String args[])
    {
      try
      {
          int i = 0;
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("C:/results.txt");
          // Stream to write file
          FileOutputStream fout = new FileOutputStream("C:/count_results.txt");     
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          //Read File Line By Line
          while ((strLine = br.readLine()) != null)   
          {
              if (strLine.contains("CREATE PROCEDURE"))
              {
                  while (!strLine.contains("END PROCEDURE;"))
                  {
                      i++;
                      break;
                  }
                  new PrintStream(fout).println (i);
              }
          }
          //Close the input stream
          in.close();
          fout.close();
      }
      catch (Exception e)
      {
          //Catch exception if any
          System.err.println("Error: " + e.getMessage());
      }
    }
}

However, that just counts the total number of instances of "CREATE PROCEDURE" and writes out the variable i each time it gets incremented...not what I'm looking for.

Any help?

SOLUTION FOUND:

import java.io.*;

public class countFile 
{
    public static void main(String args[])
    {
      try
      {
          int i = 0;
          boolean isInProcedure = false;
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("C:/results.txt");
          // Stream to write file
          FileOutputStream fout = new FileOutputStream("C:/count_results.txt");     
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          // Read File Line By Line
          while ((strLine = br.readLine()) != null)   
          {
              // If the line contains create procedure...
              if (strLine.contains("CREATE PROCEDURE")) 
              {
                  // Set this flag to true
                  isInProcedure = true;
              }
              // If the line contains end procedure, stop counting...
              if (strLine.contains("END PROCEDURE;")) 
              {
                 // Write to the new file
                 new PrintStream(fout).println(i);
                 // Set flag to false
                 isInProcedure = false;
                 // Reset counter
                 i = 0;
              }
              // Used to count lines between create procedure and end procedure
              else
              {
                  if (isInProcedure == true)
                  {
                      //Increment the counter for each line
                      i++;
                  }
              }
          }
          //Close the input stream
          in.close();
          fout.close();
      }
      catch (Exception e)
      {
          //Catch exception if any
          System.err.println("Error: " + e.getMessage());
      }
    }
}

Upvotes: 0

Views: 1144

Answers (2)

isvforall
isvforall

Reputation: 8926

Try this:

while (br.ready()) {
   if (br.readLine().contains("CREATE PROCEDURE")) {
      while (!br.readLine().contains("END PROCEDURE;")) {
         i++;
      }
    }
    new PrintStream(fout).println(i);
    i = 0;
}

Upvotes: 2

Jonathan Payne
Jonathan Payne

Reputation: 2223

import java.io.BufferedReader;
import java.io.FileReader;

public class Test
{
    public static void main( String args[] )
    {
        try
        {
            BufferedReader bufferedReader = new BufferedReader( new FileReader( "c:\\test.txt" ) );

            String line;
            Boolean count = false;
            Integer lines = 0;

            while ( ( line = bufferedReader.readLine() ) != null )
            {
                if ( line.toUpperCase().contains( "CREATE PROCEDURE" ) )
                {
                    count = true;
                }

                if ( line.toUpperCase().contains( "END PROCEDURE;" ) )
                {
                    break;
                }

                if ( count == true )
                {
                    lines++;
                }
            }

            System.out.println( lines );
        }
        catch ( Exception exception )
        {
            exception.printStackTrace();
        }
    }
}

Upvotes: 1

Related Questions