Java:如何使缓冲阅读器遍历输入文件,并运行一次代码块



...对不起,长度...和愚蠢的代码/文本条目。我还是个菜鸟。

我需要解析 Trimble 差分校正日志文件(下面的示例(获取指定的值,并将它们放入 .csv 文件中以进行 QA,然后再上传到 Oracle。我选择Java进行专业开发,因为它是用于开发其他内部软件的语言。

我指向单个文件,直到我可以得到正确的输出,然后我将循环访问文件结构。

我的要求:读取日志(.txt UTF-16LE(并从几个几乎相似的文本块中获取特定值;然后在随后的几乎相似(但与第一个块不同(的文本块中找到其他值。将这些值放入.csv中,以便导入到电子表格中,以便按日志文件进行 QA。

文本块中的值可能会有所不同,但所有可能的变体都是已知的。

我只关心第一块文本,ATM。我感兴趣的所有值的 REGEX 如下。

import java.io.*;
import java.nio.*;
import java.util.*;
import java.util.regex.*;
public class LogParser
{
public static void main (String[] args)throws IOException
{
//log file Reader init:
File corrFile = new File("D:\Utilities\Development\Java\HPGPSLogParser\Correct_2015-10-13_10-51.txt");
BufferedReader corrReader = new BufferedReader(new InputStreamReader(new FileInputStream(corrFile),"UTF-16LE"));
String corrText = "";
String corrLine = "";
/*
NOTE: PFO diffcorr log files are encoded in UTF-16 LE
*/
//Writer init:
File stateCSV = new File("D:\Utilities\Development\Java\HPGPSLogParser\MH.csv");
BufferedWriter corrWriter = new BufferedWriter(new FileWriter(stateCSV, true));
String fileText = "";
//output reader variables:
String corrOutput = "";
String outputLine = "";
//Management variables: ID location & specify actions:
String roverFile = "Rover file: ";
String procRoverFile = "Processing rover file, ";
String carrProcess = "";
String codeProces = "";
//regex variables:
Pattern fileName1 = Pattern.compile("Rover file: (?<fileName1>[A-Z]{2}-\d{3}-\d{5}-SP\d\.SSF)+");
Pattern noBase = Pattern.compile("(?<noBase>No matching base data found)");
Pattern totalCoverage = Pattern.compile("(?<totalCoverage>[\d]{1,3})\% total coverage");
Pattern coverageBy = Pattern.compile("(?<coverageBy>[\d]{1,3})+\% coverage by (?<baseStation>\b\w+\b\.[zZ].*)+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Pattern carrierProcessing = Pattern.compile("Carrier processing\.\.\.");
Pattern fileName2 = Pattern.compile("Processing rover file, (?<fileName2>[A-Z]{2}-\d{3}-\d{5}-SP\d\.SSF)+ \.\.\.");
Pattern noProc = Pattern.compile("(?<noProc>No processing performed as base data does not have carrier data)");
Pattern noCarDat = Pattern.compile("(?<noCarDat>No carrier processing performed as file has no carrier data)");
Pattern selectedPositions = Pattern.compile("Selected (?<selectedPositions>\d{1,6}) positions for post-processing");
Pattern correctedPositions = Pattern.compile("Corrected (?<correctedPositions>\d{1,6}) positions");
Pattern correctFailed = Pattern.compile("Failed to correct (?<correctFailed>\d{1,6}) positions");
Pattern carrierMissingBase = Pattern.compile("(?<carrierMissingBase>\d{1,6}) of these were due to missing base data");
Pattern carrierInsuffSat = Pattern.compile("(?<carrierInsuffSat>\d{1,6}) of these were due to insufficient satellites for position fix");
Pattern codeProcessing = Pattern.compile("Code processing\.\.\.");
Pattern refGap = Pattern.compile("(?<refGap>Reference station data gap encountered: )");
Pattern codeChose = Pattern.compile("Chose (?<codeChose>\d{1,6}) code solutions over the carrier solutions");
Pattern codeHighQual = Pattern.compile("(?<codeHighQual>\d{1,6}) code solutions were of higher quality");
Pattern filtered = Pattern.compile("Filtered out (?<filtered>\d{1,6}) uncorrected positions");
try(corrReader)
{
while ((corrLine = corrReader.readLine())!=null)
{
corrText = corrLine.trim();
Matcher carrProcMatcher = carrierProcessing.matcher(corrText);
if (corrText.contains(roverFile))
{
Matcher file1Matcher = fileName1.matcher(corrText); //first order variable based on 'Rover file: fileName1'
if(file1Matcher.find())
{
String firstFileName = file1Matcher.group("fileName1");
if (corrOutput.equals(""))
{
corrOutput += firstFileName+",";
} else {
corrOutput += "n"+firstFileName+",";
} //end else
Matcher baseMatcher = noBase.matcher(corrText);
if(baseMatcher.find()) 
{
String noBaseText = baseMatcher.group("noBase");
if(noBaseText.equals("No matching base data found"))
{
corrOutput += "TRUE"+",";
} else {
corrOutput += ",";
} //end else
} 
Matcher totCovMatcher = totalCoverage.matcher(corrText);
if(totCovMatcher.find()) 
{
String totalCovText = totCovMatcher.group("totalCoverage");
corrOutput += totalCovText+",";
}
Matcher covByMatcher = coverageBy.matcher(corrText);
if(covByMatcher.find()) 
{
String covByPct = covByMatcher.group("coverageBy");
String covByProvider = covByMatcher.group("baseStation");
corrOutput += covByPct+","+covByProvider+",";
}
corrWriter.write(corrOutput);
corrWriter.flush();
} // end file1Matcher if
} //end corrText.contains if
} //end while loop
//              corrWriter.write(corrOutput);
corrWriter.close();
corrReader.close();
} //end try corrReader
} //end main method
} //end class

我感兴趣的日志文件的日志内容如下所示:

--------承保范围详情:--------------------

漫游车文件:AA-123-12345-SP1。小规模渔业

当地时间: 2/11/2014 8:06:30 PM 至 2/11/2014 8:37:15 PM

100%全覆盖

guug04314054 覆盖率 100%.zip

漫游车文件:AA-321-54321-SP1。小规模渔业

当地时间: 2/3/2015 4:06:14 PM 至 2015/2/3 16:06:44

0% 总覆盖率。 未找到匹配的基础数据。

漫游车文件:AA-132-12354-SP2。小规模渔业

当地时间: 2/17/2014 5:51:01 PM 至 2014/2/17 18:18:57 PM

100%全覆盖

4% 覆盖率 guug04914003.zip

guug04914022 覆盖率 100%.zip

我需要我的输出看起来像:

AA-123-12345-SP1.SSF,,100,100,guug04914003.zip,

AA-312-12435-SP1.SSF,真,0,,,

我的代码多次循环遍历输入文件,生成重复条目。如何为每个"Rover 文件:"文本块获取单个输出条目?

谢谢!!

只遍历文件一次。随时读取和收集数据。在获得所有数据之前,不要生成输出。当您看到新的Rover file条目时,写入输出(除非第一个条目(并清除值。到达末尾时,写入输出(如果有(。

隔离类中的代码可能会使重用打印逻辑变得更加容易。

例:

public final class LogEntry {
private final Pattern pattern = Pattern.compile("Rover file: (.*)" +
"|(\d+)% total coverage" +
"|(\d+)% coverage by (.*)");
private String roverFile;
private Integer totalCoverage;
private Map<String, Integer> fileCoverage = new LinkedHashMap<>();
public void process(BufferedReader in) throws IOException {
for (String line; (line = in.readLine()) != null; ) {
Matcher m = this.pattern.matcher(line);
if (! m.matches())
continue;
if (m.start(1) != -1) {
print();
clear();
this.roverFile = m.group(1);
} else if (m.start(2) != -1) {
this.totalCoverage = Integer.valueOf(m.group(2));
} else if (m.start(3) != -1) {
this.fileCoverage.put(m.group(4), Integer.valueOf(m.group(3)));
}
}
print();
}
private void clear() {
this.roverFile = null;
this.totalCoverage = null;
this.fileCoverage.clear();
}
private void print() {
if (this.roverFile == null)
return;
if (this.fileCoverage.isEmpty()) {
System.out.println(this.roverFile + "," + this.totalCoverage);
} else {
for (Entry<String, Integer> entry : this.fileCoverage.entrySet()) {
System.out.println(this.roverFile + "," + this.totalCoverage + "," + entry.getValue() + "," + entry.getKey());
}
}
}
}

测试

String input = "Rover file: AA-123-12345-SP1.SSFn" +
"Local time: 2/11/2014 8:06:30 PM to 2/11/2014 8:37:15 PMn" +
"100% total coveragen" +
"100% coverage by guug04314054.zipn" +
"Rover file: AA-321-54321-SP1.SSFn" +
"Local time: 2/3/2015 4:06:14 PM to 2/3/2015 4:06:44 PMn" +
"0% total coverage. No matching base data found.n" +
"Rover file: AA-132-12354-SP2.SSFn" +
"Local time: 2/17/2014 5:51:01 PM to 2/17/2014 6:18:57 PMn" +
"100% total coveragen" +
"4% coverage by guug04914003.zipn" +
"100% coverage by guug04914022.zipn";
try (BufferedReader in = new BufferedReader(new StringReader(input))) {
new LogEntry().process(in);
}

输出

AA-123-12345-SP1.SSF,100,100,guug04314054.zip
AA-321-54321-SP1.SSF,0
AA-132-12354-SP2.SSF,100,4,guug04914003.zip
AA-132-12354-SP2.SSF,100,100,guug04914022.zip

相关内容

最新更新