I have written a program that parses a csv file and creates a bean from the data to be put into a database. Everything works perfectly however now that this will be moved out of a testing environment, the real header names from the csv's will have to be added. These headers contain spaces and /. I am searching for a way to allow my parser to read these headers. When I define the header names, I have to use camelCasing and I am unable to insert spaces or other characters. Is there anyway to alter this?
Here is my constructor(integrationTeam needs to be Integration Team, softwareHardware needs to be Hardware/Software -- as is in 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;
}
Here is the parser that handles the constructor
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;
}
}
See the Super CSV documentation, section Reading with 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.
You read the header and pass it to beanReader.read()
as the second parameter. But according to the API reference the second parameter is a string array containing the bean property names. So you should pass something like
new String[] { "name", "manufacturer", "model", "owner", "integrationTeam", ... }
as the second parameter. So the first CSV column matches to bean field name
, the second field matches to bean field manufacturer
, etc.