在Java中读取CSV文件时跳过第一行
我正在编写一个解析器代码来读取.csv文件并将其解析为XML。这是我的代码,它可以工作,除了我希望它跳过文件中的第一行。所以我决定设置一个HashMap,但它似乎不起作用:在Java中读取CSV文件时跳过第一行,java,csv,line,skip,Java,Csv,Line,Skip,我正在编写一个解析器代码来读取.csv文件并将其解析为XML。这是我的代码,它可以工作,除了我希望它跳过文件中的第一行。所以我决定设置一个HashMap,但它似乎不起作用: for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (file.isFile() && file.getName().endsWith(".csv"
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++;
for(int i=0;i
这是代码主要部分的一个片段。有什么想法或解决方案吗?创建一个变量
interaction
并用0
初始化。在循环时,首先检查它
String line;
int iteration = 0;
while ((line = br.readLine()) != null) {
if(iteration == 0) {
iteration++;
continue;
}
...
...
}
我被你的代码弄糊涂了,你有线条图,还有fw(不管是什么)。你在用哪一个?你说你想跳过第一行,但你没有
if (firstLine == true) {
firstLine = false;
continue;
}
我还建议使用像CSVReader这样的库,我相信它甚至有一个属性ignoreFirstLine
为什么不直接使用for循环呢
for(int i=1; (line = br.readLine()) != null; i++)
{
//Your code
}
你可以考虑在你的while循环之前放置<代码>标题行= BR.ReadRead()/Cux>,这样你就可以从文件的其余部分单独地使用头文件。你也可以考虑使用CSV解析,因为它可以简化你的逻辑。
使用缓冲读写器两次,比如:
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null) {
//your code
}
}
为了跳过第一行(通常包含列的标题),首先在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)
}
我觉得有必要添加一个Java8风格的答案
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());
List xmlLines=new BufferedReader(新文件读取器(csvFile))
.行()
.skip(1)//跳过前n行,在本例中为1
.map(s->{
//csv行解析和xml逻辑
//...
返回xmlString;
})
.collect(Collectors.toList());
不是添加计数器,而是添加标志不会影响性能。一种简单的方法,声明一个变量并为其赋值(例如int k=0;
),并在进入循环后立即增加变量值。下面给出了代码
BufferedReader csvReader = new BufferedReader(new FileReader("mycsv.csv"));
// declare a variable
int k=0;
while ((row = csvReader.readLine()) != null) {
if(k == 0){
k++;
continue;
}
//rest of your code
// inside while loop
}
不是你的反对票,而是澄清你的“它似乎确实有效”
,请。对不起,打字错误……这似乎不起作用。我们知道这一点,但在将来,请详细说明它是如何起作用的。你提供的信息越多,回答你的问题就越容易。如果我想跳过前两行怎么办?一行是文本,下一行是空行/空白?@KalaJ:Do如果(iteration==0 | | iteration==1)
相反,或者更好的如果(iteration<2)
。明白了吗?同样的基本答案在一年多前就发布了。如何从第二行读取初始化i=1,对变量i没有控制,使用i在这里是过时的。
BufferedReader csvReader = new BufferedReader(new FileReader("mycsv.csv"));
// declare a variable
int k=0;
while ((row = csvReader.readLine()) != null) {
if(k == 0){
k++;
continue;
}
//rest of your code
// inside while loop
}