Java CsvReader无法获取第一列

时间:2017-02-27 21:13:28

标签: java csv

我正在使用JavaCSV来读写CSV文件,我发现一个问题是它忽略了程序部分第一列中的数据

String nNumber = planes.get(" N-NUMBER");即使数据库中存在某些内容,也不会返回任何内容

CsvReader http://javacsv.sourceforge.net/com/csvreader/CsvReader.html

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.String;
import java.io.File;
import java.io.FileWriter;
import java.lang.String;
import com.csvreader.CsvReader;

private static void compare(String firstName, String lastName, String st1, String st2, String city, String state, String zip,
        String country, String region, String fileName, Integer pilotCount) 
{
    // TODO Auto-generated method stub
    try {

        CsvReader planes = new CsvReader("MASTER.csv");
        planes.readHeaders();

        while (planes.readRecord())
        {
            String nNumber = planes.get("N-NUMBER");
            String SN = planes.get("SERIAL NUMBER");
            String model = planes.get("MFR MDL CODE");
            String engine = planes.get("ENG MFR MDL");
            String year = planes.get("YEAR MFR");
            String typeReg = planes.get("TYPE REGISTRANT");
            String name = planes.get("NAME");
            String st_1 = planes.get("STREET");
            String st_2 = planes.get("STREET2");
            String city_plane = planes.get("CITY");
            String pilot_state = planes.get("STATE");
            String zip_code = planes.get("ZIP CODE");
            String country_name = planes.get("COUNTRY");
            String region_name = planes.get("REGION");
            //boolean response = compare(st1, st2, city, state, zip, country, region, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name);
            boolean response = compareSimple(firstName, lastName, name);

            if (response == true)
            {
                System.out.println("N Number");
                System.out.println(nNumber);
                System.out.println("Serial");
                System.out.println(SN);
                System.out.println("Model");
                System.out.println(model);
                System.out.println("Engine");
                System.out.println(engine);
                System.out.println("Year");
                System.out.println(year);
                System.out.println("Type Registration");
                System.out.println(typeReg);
                System.out.println("Name");
                System.out.println(name);
                pilotCount++;
                write(nNumber, SN, model, engine, year, typeReg, name, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name, fileName);
                System.out.println(pilotCount);
            }
            else if (response == false)
            {
                // do nothing
            }
        }
        planes.close();
    }
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

可能与this one相关吗?

您可能使用不正确的编码来读取此信息,或者您的文件具有BOM标记,在这种情况下,文件以几个字节开头,表示编码是什么。在这种情况下,您可以跳过这些字节以获得正确的输出:

InputStream in = new FileInputStream("/path/to/file.csv", "UTF-8");

//this skips UTF-8 BOM bytes, adjust to discard the bytes of whatever you have:

if(in.read() == 239 & in.read() == 187 & in.read() == 191){
    System.out.println("UTF-8 with BOM, bytes discarded");
}

//parse the InputStream now as the BOM bytes have been skipped.

如果您确定不是这种情况并且没有BOM标记,请尝试切换到另一个库以查看问题是否仍然存在。