在 Java 中读取 CSV 文件时跳过第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18306270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip first line while reading CSV file in Java
提问by user2643355
Hey guys I am writing a parser code to read a .csv file and parse it to XML. This is the code I have and it works, except I would like it to skip the first line in the file. So I decided to set up a HashMap but it doesn't seem to work:
大家好,我正在编写解析器代码来读取 .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<Integer,String> lineMap = new HashMap<Integer,String>();
int k=2;
while ((line = br.readLine()) != null) {
System.out.println(n + " iteration(s) of 1st While Loop");
lineMap.put(k, line);
fw.write(" <ASSET action=\"AddChange\">\n");
fw.write(" <HOSTNAME>\n");
hostName=line.substring(0, line.indexOf(","));
fw.append(hostName);
fw.write("</HOSTNAME>\n");
fw.write(" <HOSTID>\n");
hostID=line.substring(line.indexOf(",")+1, nthOccurrence(line, ',', 1));
fw.append(hostID);
fw.write("</HOSTID>\n");
fw.write(" <MACMODEL>\n");
machineModel=line.substring(nthOccurrence(line, ',', 1)+1, nthOccurrence(line, ',', 2));
fw.append(machineModel);
fw.write("</MACMODEL>\n");
fw.write(" <PROMODEL>\n");
processorModel=line.substring(nthOccurrence(line, ',', 2)+1, nthOccurrence(line, ',', 3));
fw.append(processorModel);
fw.write("</PROMODEL>\n");
fw.write(" <CORE>\n");
core=line.substring(nthOccurrence(line, ',', 3)+1, nthOccurrence(line, ',', 4));
fw.append(core);
fw.write("</CORE>\n");
fw.write(" <PROC>\n");
proc=line.substring(nthOccurrence(line, ',', 4)+1, nthOccurrence(line, ',', 5));
fw.append(proc);
fw.write("</PROC>\n");
fw.write(" <TIER>\n");
tier=line.substring(nthOccurrence(line, ',', 5)+1, nthOccurrence(line, ',', 6));
fw.append(tier);
fw.write("</TIER>\n");
fw.write(" <PRODNAME>\n");
productName=line.substring(nthOccurrence(line, ',', 6)+1, nthOccurrence(line, ',', 7));
fw.append(productName);
fw.write("</PRODNAME>\n");
fw.write(" <VERSION>\n");
version=line.substring(nthOccurrence(line, ',', 7)+1, nthOccurrence(line, ',', 8));
fw.append(version);
fw.write("</VERSION>\n");
fw.write(" <SCRIPTDATA>\n");
scriptData=line.substring(nthOccurrence(line, ',', 8)+1, line.length());
fw.append(scriptData);
fw.write("</SCRIPTDATA>\n");
fw.write(" </ASSET>\n");
k++;
}n++;
This is a snippet of the main part of the code. Any Ideas or Solutions???
这是代码主要部分的片段。任何想法或解决方案???
采纳答案by David Maust
You might consider placing headerLine = br.readLine()
before your while loop so you consume the header separately from the rest of the file. Also you might consider using opencsvfor csv parsing as it may simplify your logic.
您可能会考虑放置headerLine = br.readLine()
在 while 循环之前,以便将标头与文件的其余部分分开使用。您也可以考虑使用opencsv进行 csv 解析,因为它可以简化您的逻辑。
回答by Azodious
Create a variable interation
and initialize with 0
. Check it as very first thing in while
loop.
创建一个变量interation
并用0
. 检查它作为while
循环中的第一件事。
String line;
int iteration = 0;
while ((line = br.readLine()) != null) {
if(iteration == 0) {
iteration++;
continue;
}
...
...
}
回答by Scary Wombat
I am rather confused by your code, your have the lineMap and you also have fw (whatever that is). Which one are you using? You say you want to skip the first line, but you don't
我对你的代码感到很困惑,你有 lineMap 并且你也有 fw(不管那是什么)。你用的是哪一个?你说你想跳过第一行,但你没有
if (firstLine == true) {
firstLine = false;
continue;
}
I would also suggest using a library like CSVReader which I belive even has a property ignoreFirstLine
我还建议使用像 CSVReader 这样的库,我相信它甚至有一个属性 ignoreFirstLine
http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVReader.html
http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVReader.html
回答by achakravarty
why don't you just use the for loop
为什么不直接使用 for 循环
for(int i=1; (line = br.readLine()) != null; i++)
{
//Your code
}
回答by user3418885
Use buffer reader two times, like this:
使用缓冲区读取器两次,如下所示:
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null) {
//your code
}
}
回答by user3585491
For skipping first line(which normally contains header of the columns) take a variable and increase this variable in while loop at first place, and continue;
为了跳过第一行(通常包含列的标题),首先取一个变量并在 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)
}
回答by n4rzul
I feel compelled to add a java 8 flavored answer.
我觉得有必要添加一个 Java 8 风格的答案。
List<String> 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());
回答by rahulpatil
boolean isRecord = false;
for (CSVRecord record : records) {
if(isRecord){
//process records here.
}else{
isRecord = true;
}
}
Instead of adding counter adding flag will not hit the performance.
而不是添加计数器添加标志不会影响性能。