格式化bean生成器的头字段 - Java

时间:2015-01-26 18:54:31

标签: java parsing csv supercsv

我编写了一个解析csv文件的程序,并根据要放入数据库的数据创建bean。一切都运行良好但是现在这将被移出测试环境,必须添加来自csv的真实标题名称。这些标头包含空格和/。我正在寻找一种方法来允许我的解析器读取这些头文件。当我定义标题名称时,我必须使用camelCasing,我无法插入空格或其他字符。反正有改变这个吗?

这是我的构造函数(integrationTeam需要是Integration Team,softwareHardware需要是硬件/软件 - 就像在csv头中一样)

public class BeanGen {

public BeanGen(
    final String name, 
    final String manufacturer,
    final String model, 
    final String owner,
    final String integrationTeam, 
    final String shipping,
    final String hardwareSoftware, 
    final String subsystem,                      
    final String plane, 
    final String integrationStandalone,  
    final String integrationInterface, 
    final String function, 
    final String helpLinks, 
    final String installationInstructions, 
    final String testSteps, 
    final String leadEngineer)
    {
    this.name = name;
    this.manufacturer = manufacturer;
    this.model = model;
    this.owner = owner;
    this.integrationTeam = integrationTeam;
    this.shipping = shipping;
    this.hardwareSoftware = hardwareSoftware;
    this.subsystem = subsystem;
    this.plane = plane;
    this.integrationStandalone = integrationStandalone;
    this.integrationInterface = integrationInterface;
    this.function = function;
    this.helpLinks = helpLinks;
    this.installationInstructions = installationInstructions;
    this.testSteps = testSteps;
    this.leadEngineer = leadEngineer;
    }

这是处理构造函数的解析器

public class ParseHandler {

private static CellProcessor[] getProcessors() {

    final CellProcessor[] processors = new CellProcessor[] {

            new Optional(), 
            new Optional(), 
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
            new Optional(), 
            new Optional(),
    };
    return processors;
}

public static BeanGen readWithCsvBeanReader(Path path) throws IOException {
    ICsvBeanReader beanReader = null;
    BeanGen projectBean = null;
    System.out.println("Processing File: " + path);
    try {
        beanReader = new CsvBeanReader(new FileReader(path.toString()),   CsvPreference.STANDARD_PREFERENCE);
        // the header elements are used to map the values to the bean (names
        // must match)
        final String[] header = beanReader.getHeader(true);
        final CellProcessor[] processors = getProcessors();

        if ((projectBean = beanReader.read(BeanGen.class, header, processors)) != null) {
            System.out.println(String.format("%s", projectBean.toString()));
        }
    } finally {
        if (beanReader != null) {
            beanReader.close();
        }
    } return projectBean;
}
}

1 个答案:

答案 0 :(得分:2)

请参阅Super CSV documentation使用CsvBeanReader阅读部分:

  

这依赖于以下事实:CSV文件的标题中的列名与bean的字段名完全匹配,并且bean具有为每个字段定义的适当的setter。   如果您的标题不匹配(或没有标题),那么您只需定义自己的名称映射数组。

您阅读标题并将其作为第二个参数传递给beanReader.read()。但根据API reference,第二个参数是一个包含bean属性名称的字符串数组。所以你应该传递像

这样的东西
new String[] { "name", "manufacturer", "model", "owner", "integrationTeam", ... }

作为第二个参数。所以第一个CSV列匹配bean字段name,第二个字段匹配bean字段manufacturer等。