Java 计算文本文件中从点A到点B的行数
我试图找出如何在给定的文本文件中找到字符串的第一个实例,开始计算从该字符串实例的第一行开始的行数,然后在新行中找到另一个/不同的字符串时停止计算 基本上我有一个文本文件,如:Java 计算文本文件中从点A到点B的行数,java,loops,io,count,text-files,Java,Loops,Io,Count,Text Files,我试图找出如何在给定的文本文件中找到字符串的第一个实例,开始计算从该字符串实例的第一行开始的行数,然后在新行中找到另一个/不同的字符串时停止计算 基本上我有一个文本文件,如: CREATE PROCEDURE "dba".check_diff(a smallint, b int, c int, d
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;
我只想找到“createprocedure”的第一个实例,然后递增一个变量,直到到达“endprocedure;”的实例,然后重置变量
我一直在尝试的是
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());
}
}
}
然而,这只是计算“createprocedure”的实例总数,并在每次递增时写出变量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());
}
}
}
试试这个:
while (br.ready()) {
if (br.readLine().contains("CREATE PROCEDURE")) {
while (!br.readLine().contains("END PROCEDURE;")) {
i++;
}
}
new PrintStream(fout).println(i);
i = 0;
}
试试这个:
while (br.ready()) {
if (br.readLine().contains("CREATE PROCEDURE")) {
while (!br.readLine().contains("END PROCEDURE;")) {
i++;
}
}
new PrintStream(fout).println(i);
i = 0;
}
似乎没有给我一个准确的数字。刚刚写了多次“1”:(@This0nePr0grammer Ohh…抱歉,现在它很好用,试试这一个哈哈,这似乎把它扔进了一个无限循环中,我从不递增,也就是说,0只是一次又一次地打印出来。@This0nePr0grammer对不起我的英语,我不懂你的意思,这是否有效?不,它不起作用,它导致了一个无限循环。:(但不久前我想出了一个解决方案,我感谢你的帮助。似乎没有给我一个准确的数字。只是写了多次“1”。)(@This0nePr0grammer Ohh…抱歉,现在它很好用,试试这一个哈哈,这似乎把它扔进了一个无限循环中,我从不递增,也就是说,0只是一次又一次地打印出来。@This0nePr0grammer对不起我的英语,我不懂你的意思,这是否有效?不,它不起作用,它导致了一个无限循环。:(但不久前我找到了一个解决方案,我感谢您的帮助。遗憾的是,这似乎也不起作用。它只是给了我一个“创建过程”不同实例的总数,这不是我想要的:(.但还是谢谢你。@This0nePr0grammer什么?它的输出是18。这正是你所要求的。也许我的问题措辞不好?我基本上有多个以“create procedure”和“end procedure”开头的语句我需要计算一个文本文件中多个语句的两行之间的行数。我最终找到了解决方案,但部分要感谢你的帖子。遗憾的是,这似乎也不起作用。它只是给了我一个“创建过程”不同实例的总计数这不是我想要的:(。但还是谢谢你。@This0nePr0grammer什么?它的输出为18。这正是你要求的。也许我的问题措辞不好?我基本上有多个以“create procedure”和“end procedure”开头的语句我需要计算一个文本文件中多个语句的两行之间的行数。我最终找到了解决方案,但部分要感谢你的帖子。