我想使用Java从输入文件中逐行读取。代码的逻辑应为:
- 日志类中的loadFile()读取第一行。 deltaRecords存储第一行
- 在Main class中,我调用loadFile(),它仅将数据上传到第一行的deltaRecords中。 loadFile()等待,直到第一行由Mainclass中的testRecords()
分析- 日志类中的loadFile()读取第二行。 deltaRecords存储第二行
- 在Main class中,我调用loadFile(),它仅将数据上传到第二行的deltaRecords中。 loadFile()等待,直到第二行由Mainclass中的testRecords()
分析- 日志类中的loadFile()读取第三行。 deltaRecords存储第三行。
例如,我的输入文件是:
TOM 1 2 <br/>
Jason 2 3 <br/>
Tommy 1 4 <br/>
阅读TOM 1 2.之后,我需要停止该过程并进行分析。然后,我继续阅读Jason 23。
这是我当前的代码。在log class中,我定义了loadFile()逐行读取输入。在Main class中,我定义了testRecords()进行分析。:
public void loadFile(String filename) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
Main main = new Main();
String line = br.readLine();
int count = 0;
while (line != null) {
if (line.length() == 0) {
line = br.readLine();
continue;
}
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read
//initial the deletaRecords and add current line to the old record
existRecords.add(record);
line = br.readLine();
}
}catch(Exception e){
System.out.println("load log file failed! ");
e.printStackTrace();
}
在我的主函数中,我调用loadfile:
Log log = new Log();
log.loadFile("data/output_01.txt");
QueryEngine.queryEngine.log = log;
testRecord(log);
有人可以告诉我如何解决此问题吗?非常感谢。
替换
String line = br.readLine();
with
String line = null;
替换
while (line != null) {
with
while ((line = br.readLine()) != null) {
替换
if (line.length() == 0) {
line = br.readLine();
continue;
}
with
if (line.length() == 0) {
continue;
}
String line = null;
while ((line = br.readLine()) != null) {
if (line.length() == 0) continue;
//Analyze your line
}
您可以使用Consumer lambda函数作为loadFile上的输入来解决它,如下所示:
public void loadFile(String filename, Consumer<LogRecord> lineAnalyzer) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
Main main = new Main();
String line = br.readLine();
int count = 0;
while (line != null) {
if (line.length() == 0) {
line = br.readLine();
continue;
}
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read
//Analyze line
lineAnalyzer.accept(record);
existRecords.add(record);
line = br.readLine();
}
}catch(Exception e){
System.out.println("load log file failed! ");
e.printStackTrace();
}
然后,当您调用loadFile函数时,可以如下指定分析行逻辑:
Log log = new Log();
QueryEngine.queryEngine.log = log;
log.loadFile("data/output_01.txt",logRecord -> {
System.out.println("Starting analysis on log record "+logRecord);
// Analyze the log record
testRecord(log);
});
您可以使用扫描仪逐行读取文件并进行处理。我在下面附加了代码段。
public void loadFile(String filename) {
try {
Scanner sc= new Scanner(new File(filename));
while (sc.hasNext()) {
String line = sc.nextLine();
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read next line
//Place your code here
}
} catch (FileNotFoundException fe) {
System.err.println(fe);
}
}