这是文本文件
gr.spinellis.ckjm.ClassVisitor 13 2 0 14 74 34 2 9
gr.spinellis.ckjm.ClassMetricsContainer 3 1 0 3 18 0 2 2
gr.spinellis.ckjm.MetricsFilter 7 1 0 6 30 11 2 5
gr.spinellis.ckjm.PrintPlainResults 2 1 0 2 8 0 1 2
gr.spinellis.ckjm.MethodVisitor 11 2 0 21 40 0 1 8
gr.spinellis.ckjm.CkjmOutputHandler 1 1 0 1 1 0 3 1
这是我编写的用于解析的代码,我想跳到值之间的一行,顺便说一下,在进入while循环之前,我已经通过读取一行来跳过第一行。
package javaapplication39;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/* to read CSV file in Java. In this program we will read * list of metrics stored in CSV file as comma separated values. */
public class readallvalues {
public static void main(String... args) {
List<Metrics> metric = readMetricFromCSV("C:\\Users\\hp\\Desktop\\1.txt");
// let's print all the metric read from CSV file
for (Metrics m : metric) {
System.out.println(m);
}
}
private static List<Metrics> readMetricFromCSV(String fileName) {
List<Metrics> metricsss = new ArrayList<>();
Path pathToFile = Paths.get(fileName);
// create an instance of BufferedReader
// using try with resource, Java 7 feature to close resources
try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {
br.readLine();
String line1=null;
// read the first line from the text file
String line = br.readLine();
while (line != null) { // loop until all lines are read
String[] attributes = line.split(" "); // the file, using a comma as the delimiter
Metrics valueOfMetric = createMetric(attributes);
metricsss.add(valueOfMetric); // adding metric into ArrayList
//skip empty line
// line.isEmpty() || line.trim().equals("") || line.trim().equals("\n"))
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return metricsss;
}
private static Metrics createMetric(String[] metadata) {
String name = metadata[0];
int WMC = Integer.parseInt(metadata[1]);
int DIT = Integer.parseInt(metadata[2]);
int NOC = Integer.parseInt(metadata[3]);
int CBO = Integer.parseInt(metadata[4]);
int RFC = Integer.parseInt(metadata[5]);
int LCOM= Integer.parseInt(metadata[6]);
int Ce = Integer.parseInt(metadata[7]);
int NPM = Integer.parseInt(metadata[8]);
return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ce,NPM);//,cc
}
}
class Metrics {
private String name;
private int WMC;
private int DIT;
private int NOC;
private int CBO;
private int RFC;
private int LCOM;
private int Ce;
private int NPM;
public Metrics( String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM, int Ce, int NPM) {
this.name = name;
this. WMC =WMC ;
this. DIT =DIT ;
this. NOC = NOC;
this. CBO =CBO ;
this. RFC = RFC;
this.LCOM = LCOM;
this.Ce =Ce ;
this. NPM = NPM;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getWMC() { return WMC ; }
public void setWMC(int WMC) { this.WMC = WMC ; }
//WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC ,cc
public int getDIT() { return DIT ; }
public void setDIT(int DIT) { this.DIT = DIT ; }
public int getNOCC() { return NOC ; }
public void setNOC(int NOC) { this.NOC = NOC ; }
public int getCBO() { return CBO ; }
public void setCBO(int CBO) { this.CBO = CBO ; }
public int getRFC() { return RFC ; }
public void setRFC(int RFC) { this.RFC = RFC ; }
public int getLCOM() { return LCOM ; }
public void setLCOM(int LCOM) { this.LCOM = LCOM ; }
public int getCe() { return Ce ; }
public void setCe(int Ce) { this.Ce = Ce ; }
public int getNPM() { return NPM ; }
public void setNPM(int NPM) { this.NPM = NPM ; }
@Override
public String toString() {
return "name= " + name +" WMC= " + WMC + " DIT= " + DIT + " NOC " + NOC + " CBO " + CBO
+ " RFC " + RFC + " LCOM " + LCOM + " Ce " + Ce + " NPM " + NPM +"\n\n" ;//+ " cc " + cc
}
}
答案 0 :(得分:0)
您正在循环之外阅读br.readLine();
,在资源声明的尝试下方,跳过第一行。
在br.readLine()
循环结束时添加while
调用,以跳过空行。问题是如果你的文件有两个连续的空行而不是一行,它也会跳过文本行。
while ((line = br.readLine()) != null) {
//..logic
br.readLine();//add call to skip this line
}
您最好检查线是否为空以执行逻辑。
while ((line = br.readLine()) != null) {
//trim will remove leading and trailing white space characters
if(!"".equals(line.trim()) {
//..logic
}
}
除此之外,您应遵循命名约定,类名称应以大写字母开头。因此,您的班级名称应为ReadAllvalues
。