简体   繁体   English

计算文本文件中从A点到B点的行数

[英]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. 但是,这只是计算“创建过程”的实例总数,并在变量每次递增时写出变量i ...不是我要寻找的变量。

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());
      }
    }
}

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;
}
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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM