gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:23:01 26 4
gpt4 key购买 nike

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

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

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

最佳答案

试试这个:

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

关于java - 计算文本文件中从 A 点到 B 点的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com