Java-读取文本文件
我有一个文本文件,如下所示:Java-读取文本文件,java,regex,text-files,bufferedreader,Java,Regex,Text Files,Bufferedreader,我有一个文本文件,如下所示: Past Dues / Refunds / Subsidy Arrears / Refunds Amount 2013.23 Period to which it relates Since OCT-15 现在,如何提取下一行金额中的数据。 我试过使用布尔值,检查上面和下面的行 还有别的办法吗 我的代码: boolean isGroup=false; while(line = br.readline() != null){ if(line.equals(
Past Dues / Refunds / Subsidy
Arrears / Refunds
Amount
2013.23
Period to which
it relates
Since OCT-15
现在,如何提取下一行金额中的数据。
我试过使用布尔值,检查上面和下面的行
还有别的办法吗
我的代码:
boolean isGroup=false;
while(line = br.readline() != null){
if(line.equals("Amount"){
isGroup=true;
}
if(line.equals("Period to which") && isGroup)
isGroup=false;
if(isGroup){
//read line and check whether it is null or not
String amount = line;
}
}
请帮忙。谢谢你,你的方法很好。您犯了一个小错误,设置了布尔值,然后在循环的同一次迭代中使用 如果您执行以下操作,您应该很好:
String amount = "No amount found";
boolean isGroup=false;
while(line = br.readline() != null) {
// Check all your conditions to see if this is the line you care about
if(isGroup){
amount = line;
isGroup = false; // so you only capture this once
continue;
}
else if (isOtherCondition) {
// handle other condition;
isOtherCondition = false; // so you only capture this once
continue;
}
// Check the contents of lines to see if it's one you want to read next iteration
if(line.equals("Amount"){
isGroup=true;
}
else if (line.equals("Some Other Condition")) {
isOtherCondition = true;
}
}
这就是你所需要的。休息;这样你就不必担心拿到钱后会发生什么。你的方法很好。您犯了一个小错误,设置了布尔值,然后在循环的同一次迭代中使用 如果您执行以下操作,您应该很好:
String amount = "No amount found";
boolean isGroup=false;
while(line = br.readline() != null) {
// Check all your conditions to see if this is the line you care about
if(isGroup){
amount = line;
isGroup = false; // so you only capture this once
continue;
}
else if (isOtherCondition) {
// handle other condition;
isOtherCondition = false; // so you only capture this once
continue;
}
// Check the contents of lines to see if it's one you want to read next iteration
if(line.equals("Amount"){
isGroup=true;
}
else if (line.equals("Some Other Condition")) {
isOtherCondition = true;
}
}
这就是你所需要的。休息;这样,您就不必担心获取金额后会发生什么。如果文件大小为平均大小,则可以使用正则表达式。 只需将整个文件读入一个字符串。 使用正则表达式应该是这样的。 结果为捕获组1 ?mi^\\s*金额\\s+^\\s*\\d+?:\\\.\\d*?\\\\.\\d+\\s*$
如果文件大小为平均值,则可以使用正则表达式。 只需将整个文件读入一个字符串。 使用正则表达式应该是这样的。 结果为捕获组1 ?mi^\\s*金额\\s+^\\s*\\d+?:\\\.\\d*?\\\\.\\d+\\s*$
这就是在java中执行@sln answer的方法
String text = "Past Dues / Refunds / Subsidy\n" +
"Arrears / Refunds\n" +
"Amount\n" +
"2013.23\n" +
"Period to which\n" +
"it relates\n" +
"Since OCT-15";
Pattern pattern = Pattern.compile("(?mi)^Amount\\s(?<amount>\\d+\\.\\d{2})");
Matcher matcher = pattern.matcher(text);
if(matcher.find()){
String amount = matcher.group("amount");
System.out.println("amount: "+ amount);
}
这就是在java中执行@sln answer的方法
String text = "Past Dues / Refunds / Subsidy\n" +
"Arrears / Refunds\n" +
"Amount\n" +
"2013.23\n" +
"Period to which\n" +
"it relates\n" +
"Since OCT-15";
Pattern pattern = Pattern.compile("(?mi)^Amount\\s(?<amount>\\d+\\.\\d{2})");
Matcher matcher = pattern.matcher(text);
if(matcher.find()){
String amount = matcher.group("amount");
System.out.println("amount: "+ amount);
}
因此,您尝试使用布尔值并检查上面的行。这应该行得通。你对这种方法有什么意见?你为什么想要一种不同的方法?你看到错误了吗?这个方法没有什么问题为什么会有问题?@nhouser9在问题中看到我的代码。它太长了,因为我有太多的数据要提取。有没有更短的方法?@Priyamal我正在寻找一种更短的方法,我可以直接读取下一行的金额。您使用金额读取该行,然后设置isGroup,然后在读取下一行之前将该行分配给if中的金额。因此,您尝试使用布尔值并检查上一行。这应该行得通。你对这种方法有什么意见?你为什么想要一种不同的方法?你看到错误了吗?这个方法没有什么问题为什么会有问题?@nhouser9在问题中看到我的代码。它太长了,因为我有太多的数据要提取。有没有更短的方法?@Priyamal我正在寻找一种更短的方法,我可以直接读取下一行的金额。您使用金额读取该行,然后设置isGroup,然后在读取下一行之前将该行分配给if中的金额。break;将使我脱离while循环,如果我的循环还包含一些其他任务呢。我可以使用continue;跳转到下一行迭代?是的。。。让我更新答案,以便您可以执行其他任务。例如,我想提取金额旁边的行以及与之相关的下一行。很抱歉打扰您,您能同时编写前面的代码吗?明白了!谢谢。休息;将使我脱离while循环,如果我的循环还包含一些其他任务呢。我可以使用continue;跳转到下一行迭代?是的。。。让我更新答案,以便您可以执行其他任务。例如,我想提取金额旁边的行以及与之相关的下一行。很抱歉打扰您,您能同时编写前面的代码吗?明白了!谢谢。你能详细说明一下我该如何使用这个regrex吗?@dax根据sln的答案,我在上面看到了我的答案regex@Dax-谷歌Java正则表达式。我认为您可以将文件读入字符串,然后运行FindAll或Match,它将返回一个值数组。我不使用Java正则表达式,但我一直在编写它们。这可以被充实以获得更多的记录值,但记录结构上需要更多的信息。你能详细说明我如何使用这个regrex吗?@dax根据sln的信息查看我上面的答案regex@Dax-谷歌Java正则表达式。我认为您可以将文件读入字符串,然后运行FindAll或Match,它将返回一个值数组。我不使用Java正则表达式,但我一直在编写它们。这可以充实,以获得更多的记录值,但需要在记录结构上进行更多操作。我也喜欢短的数字部分。很好。我也喜欢简短的数字部分。