在Java中读取CSV文件时跳过第一行 [英] Skip first line while reading CSV file in Java

查看:3370
本文介绍了在Java中读取CSV文件时跳过第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我写了一个解析器代码来读取一个.csv文件并解析为XML。这是我有的代码,它工作正常,除了我想它跳过文件中的第一行。所以我决定设置一个HashMap,但它似乎工作:

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 fine except I would like it to skip the first line in the file. So I decided to set up a HashMap but it does seem to work:

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???

推荐答案

您可以考虑放置 headerLine = br.readLine / code>在你的while循环之前,所以你消耗的标题与文件的其余部分分开。此外,您也可以考虑使用 opencsv 进行csv剖析,因为它可能会简化您的逻辑。

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 opencsv for csv parsing as it may simplify your logic.

这篇关于在Java中读取CSV文件时跳过第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆