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

时间:2012-05-14 21:02:09

标签: java loops io count text-files

我试图弄清楚如何在给定的文本文件中找到字符串的第一个实例,开始计算从第一行开始的行数和该字符串的实例,然后在另一个/不同的字符串时停止计数在一个新的行中找到。

基本上我有一个文本文件,如:

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;

我只是想找到“CREATE PROCEDURE”的第一个实例并递增一个变量,直到我到达“END PROCEDURE;”的实例,然后重置变量。

我一直在尝试的是......

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

然而,这只计算“CREATE PROCEDURE”的实例总数,并在每次递增时写出变量i ...而不是我正在寻找的。

任何帮助?

解决方案

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

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

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