LogParser.java
package com.mpj.Parallelization;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LogParser {
public static void main(String[] args) throws Exception {
File logfile = new File("/C:/ParallelKMeansProject/logs/log.txt");
try {
Scanner parserscanner = new Scanner(logfile);
String nextLine = parserscanner.nextLine();
String[] logcomponent = nextLine.split(" ");
String latitude = logcomponent[0];
String longtitude = logcomponent[1];
System.out.println(latitude);
System.out.println(longtitude);
System.out.println("Next Line: " + nextLine);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
log.txt内容
2017-03-10 12:30:30 DEBUG SerialKMeans:29 -
==
迭代次数:21在平方误差的簇总和内: 13.902870732812898
初始起点(随机):
Cluster 0:41.051205,28.808497 Cluster 1:41.055391,28.785407 Cluster 2:41.054838,28.802617群集3:41.022421,28.788169群集4: 41.024494,28.784099群集5:41.018659,28.787669群集6:41.040879,28.753359
全局缺失值替换为mean / mode ...
就像这样,我有log.txt和日志并排写入。
我的第一个问题是System.out.println("Next Line: " + nextLine);
我没有看到所有使用此命令行的日志。日志写在这里结束(2017-03-10 12:30:30 DEBUG SerialKMeans:29 - )不写更多。标志都是在txt文件中并排编写的。不是底线。但它只写2017-03-10 12:30:30 DEBUG SerialKMeans:29 -
我的第二个问题;如果我们解决第一个问题,我想解析一个特定的单词。我怎么能这样做
答案 0 :(得分:0)
我建议做这样的事情:
FileInputStream stream = new FileInputStream(logfile);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = br.readLine()) != null) {
System.out.println (line);
}
或者如果你想保留当前的代码,我会做@ÖmerErden所建议的,使用while循环迭代所有行。