我已經編寫了一個程序,用於解析csv文件,並根據要放入數據庫的數據創建一個bean。一切都很完美,但現在它將被移出測試環境,csv的真實標題名稱將不得不被添加。這些標題包含空格和/。我正在尋找一種方法來讓我的解析器讀取這些頭文件。當我定義標題名稱時,我必須使用camelCasing,並且我無法插入空格或其他字符。無論如何改變這個?格式化bean生成器的頭字段 - Java
這裏是我的構造函數(integrationTeam必須整合團隊,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;
}
}
解析器
您的代碼在哪裏執行csv解析並使用此構造函數? – 2015-01-26 19:13:57
請向我們展示解析代碼。你的'BeanGen'構造函數中的變量名和CSV頭名有什麼關係? – vanje 2015-01-26 19:20:33
標題元素用於將值映射到bean。標題名稱必須完全匹配,否則信息將無法找到。雖然我一直在測試這個,但我不得不改變標題名稱來顯示它們。當這個程序被使用時,我將無法這樣做,因爲有數百個csv被解析 - 所有的頭文件都是相同的,但是包含空格和字符。 – jmpman 2015-01-26 19:36:30