在Java中读取CSV文件时跳过第一行

嘿家伙我正在编写解析器代码来读取.csv文件并将其解析为XML。 这是我的代码,它工作正常,但我希望它跳过文件中的第一行。 所以我决定设置一个HashMap,但它似乎确实有效:

for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (file.isFile() && file.getName().endsWith(".csv")){ System.out.println("File Found: " + file.getName());//Prints the name of the csv file found String filePath = sourcepath + "\\" + file.getName(); BufferedReader br = new BufferedReader(new FileReader(file)); String line; int n = 1; Map lineMap = new HashMap(); int k=2; while ((line = br.readLine()) != null) { System.out.println(n + " iteration(s) of 1st While Loop"); lineMap.put(k, line); fw.write(" \n"); fw.write(" \n"); hostName=line.substring(0, line.indexOf(",")); fw.append(hostName); fw.write("\n"); fw.write(" \n"); hostID=line.substring(line.indexOf(",")+1, nthOccurrence(line, ',', 1)); fw.append(hostID); fw.write("\n"); fw.write(" \n"); machineModel=line.substring(nthOccurrence(line, ',', 1)+1, nthOccurrence(line, ',', 2)); fw.append(machineModel); fw.write("\n"); fw.write(" \n"); processorModel=line.substring(nthOccurrence(line, ',', 2)+1, nthOccurrence(line, ',', 3)); fw.append(processorModel); fw.write("\n"); fw.write(" \n"); core=line.substring(nthOccurrence(line, ',', 3)+1, nthOccurrence(line, ',', 4)); fw.append(core); fw.write("\n"); fw.write(" \n"); proc=line.substring(nthOccurrence(line, ',', 4)+1, nthOccurrence(line, ',', 5)); fw.append(proc); fw.write("\n"); fw.write(" \n"); tier=line.substring(nthOccurrence(line, ',', 5)+1, nthOccurrence(line, ',', 6)); fw.append(tier); fw.write("\n"); fw.write(" \n"); productName=line.substring(nthOccurrence(line, ',', 6)+1, nthOccurrence(line, ',', 7)); fw.append(productName); fw.write("\n"); fw.write(" \n"); version=line.substring(nthOccurrence(line, ',', 7)+1, nthOccurrence(line, ',', 8)); fw.append(version); fw.write("\n"); fw.write(" \n"); scriptData=line.substring(nthOccurrence(line, ',', 8)+1, line.length()); fw.append(scriptData); fw.write("\n"); fw.write(" \n"); k++; }n++; 

这是代码主要部分的片段。 任何想法或解决方案???

您可以考虑在while循环之前放置headerLine = br.readLine() ,以便与文件的其余部分分开使用标头。 您也可以考虑使用opencsv进行csv解析,因为它可以简化您的逻辑。

创建变量interation并使用0初始化。 在while循环中检查它是第一件事。

 String line; int iteration = 0; while ((line = br.readLine()) != null) { if(iteration == 0) { iteration++; continue; } ... ... } 

我觉得有必要添加一个java 8风格的答案。

 List xmlLines = new BufferedReader(new FileReader(csvFile)) .lines() .skip(1) //Skips the first n lines, in this case 1 .map(s -> { //csv line parsing and xml logic here //... return xmlString; }) .collect(Collectors.toList()); 

你为什么不使用for循环呢?

 for(int i=1; (line = br.readLine()) != null; i++) { //Your code } 

我对你的代码感到困惑,你有lineMap,你也有fw(无论是什么)。 你在用哪一个? 你说你想跳过第一行,但你没有

 if (firstLine == true) { firstLine = false; continue; } 

我还建议使用像CSVReader这样的库,我相信它甚至有一个属性ignoreFirstLine

http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVReader.html

使用缓冲读取器两次,如下所示:

 while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) { //your code } } 
 boolean isRecord = false; for (CSVRecord record : records) { if(isRecord){ //process records here. }else{ isRecord = true; } } 

而不是添加计数器添加标志将不会达到性能。

对于跳过第一行(通常包含列的标题),取一个变量并在while循环中增加此变量,然后继续;

 int lineNumber = 0; and then in while loop while ((line = br.readLine()) != null) { if(lineNumber == 0) { lineNumber++; continue; } lineNumber++; //do waterver u have to do with the tokens in this line(second line) }