Java - 读取文本文件
Java - Reading text file
我有一个文本文件如下:
Past Dues / Refunds / Subsidy
Arrears / Refunds
Amount
2013.23
Period to which
it relates
Since OCT-15
现在,如何提取 "Amount" 的下一行中的数据。
我已经尝试使用布尔值,检查上面和下面的行。
还有其他方法吗
我的代码:
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;
}
}
这就是您所需要的。 break;
就是让你不用担心抢到之后会发生什么。
如果文件是平均大小,您可以使用正则表达式。
只需将整个文件读入一个字符串即可。
要使用正则表达式,它会像这样。
结果在捕获组 1 中。
"(?mi)^\s*Amount\s+^\s*(\d+(?:\.\d*)?|\.\d+)\s*$"
(?mi) # Multi-line mode, case insensitive
^ # Beginning of line
\s* Amount \s+
^ # Beginning of line
\s*
( # (1 start), Numeric value
\d+
(?: \. \d* )?
| \. \d+
) # (1 end)
\s*
$ # End of line
这就是您在 java
中@sln 回答的方式
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);
}
我有一个文本文件如下:
Past Dues / Refunds / Subsidy
Arrears / Refunds
Amount
2013.23
Period to which
it relates
Since OCT-15
现在,如何提取 "Amount" 的下一行中的数据。 我已经尝试使用布尔值,检查上面和下面的行。
还有其他方法吗
我的代码:
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;
}
}
这就是您所需要的。 break;
就是让你不用担心抢到之后会发生什么。
如果文件是平均大小,您可以使用正则表达式。
只需将整个文件读入一个字符串即可。
要使用正则表达式,它会像这样。
结果在捕获组 1 中。
"(?mi)^\s*Amount\s+^\s*(\d+(?:\.\d*)?|\.\d+)\s*$"
(?mi) # Multi-line mode, case insensitive
^ # Beginning of line
\s* Amount \s+
^ # Beginning of line
\s*
( # (1 start), Numeric value
\d+
(?: \. \d* )?
| \. \d+
) # (1 end)
\s*
$ # End of line
这就是您在 java
中@sln 回答的方式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);
}