我编写了一个程序,可以解析 csv 文件并根据要放入数据库的数据创建一个 bean。一切都很完美,但是现在这将被移出测试环境,必须添加 csv 中的真实 header 名称。这些 header 包含空格和/。我正在寻找一种方法来允许我的解析器读取这些 header 。当我定义 header 名称时,我必须使用驼峰命名法,并且无法插入空格或其他字符。有什么办法可以改变这个吗?
这是我的构造函数(integrationTeam 需要是集成团队,softwareHardware 需要是硬件/软件——如 csv header 中所示)
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;
}
}
最佳答案
请参阅Super CSV documentation ,使用 CsvBeanReader 阅读部分:
This relies on the fact that the column names in the header of the CSV file [...] match up exactly with the bean's field names, and the bean has the appropriate setters defined for each field. If your header doesn't match (or there is no header), then you can simply define your own name mapping array.
您读取 header 并将其作为第二个参数传递给 beanReader.read()
。但根据API reference第二个参数是包含 bean 属性名称的字符串数组。所以你应该传递类似
new String[] { "name", "manufacturer", "model", "owner", "integrationTeam", ... }
作为第二个参数。因此,第一个 CSV 列与 bean 字段 name
匹配,第二个字段与 bean 字段 manufacturer
匹配,等等。
关于java - 格式化 Bean 生成器的 header 字段 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28156775/