Java 使用BaneUtilBean解析CSV
我试图解析csv并将字段映射到POJO类。但是,我可以看到映射没有正确实现 我正在尝试将POJO文件的头映射到csvJava 使用BaneUtilBean解析CSV,java,csv,apache-commons-beanutils,Java,Csv,Apache Commons Beanutils,我试图解析csv并将字段映射到POJO类。但是,我可以看到映射没有正确实现 我正在尝试将POJO文件的头映射到csv public class CarCSVFileInputBean { private long Id; private String shortName; private String Name; private String Type; private String Environment; //getter and sette
public class CarCSVFileInputBean {
private long Id;
private String shortName;
private String Name;
private String Type;
private String Environment;
//getter and setters
}
有人能看一下我的代码吗:
public class carCSVUtil {
private static Log log = LogFactory.getLog(carCSVUtil.class);
private static final List<String> fileHeaderFields = new ArrayList<String>();
private static final String UTF8CHARSET = "UTF-8";
static {
for (Field f : carCSVFileInputBean.class.getDeclaredFields()) {
fileHeaderFields.add(f.getName());
}
}
public static List<carCSVFileInputBean> getCSVInputList(InputStream inputStream) {
CSVReader reader = null;
List<carCSVFileInputBean> csvList = null;
carCSVFileInputBean inputRecord = null;
String[] header = null;
String[] row = null;
try {
reader = new CSVReader(new InputStreamReader(inputStream, UTF8CHARSET));
csvList = new ArrayList<carCSVFileInputBean>();
header = reader.readNext();
boolean isEmptyLine = true;
while ((row = reader.readNext()) != null) {
isEmptyLine = true;
if (!(row.length == 1 && StringUtils.isBlank(row[0]))) { // not an empty line, not even containing ','
inputRecord = new carCSVFileInputBean();
isEmptyLine = populateFields(inputRecord, header, row);
if (!isEmptyLine)
csvList.add(inputRecord);
}
}
} catch (IOException e) {
log.debug("IOException while accessing carCSVFileInputBean: " + e);
return null;
} catch (IllegalAccessException e) {
log.debug("IllegalAccessException while accessing carCSVFileInputBean: " + e);
return null;
} catch (InvocationTargetException e) {
log.debug("InvocationTargetException while copying carCSVFileInputBean properties: " + e);
return null;
} catch (Exception e) {
log.debug("Exception while parsing CSV file: " + e);
return null;
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ioe) {}
}
return csvList;
}
protected static boolean populateFields(carCSVFileInputBean inputRecord, String[] header, String[] row) throws IllegalAccessException, InvocationTargetException {
boolean isEmptyLine = true;
for (int i = 0; i < row.length; i++) {
String val = row[i];
if (!StringUtils.isBlank(val)) {
BeanUtilsBean.getInstance().copyProperty(inputRecord, header[i], val);
isEmptyLine = false;
}
}
return isEmptyLine;
}
}
公共类carCSVUtil{
私有静态日志Log=LogFactory.getLog(carCSVUtil.class);
私有静态最终列表fileHeaderFields=newarraylist();
私有静态最终字符串UTF8CHARSET=“UTF-8”;
静止的{
for(字段f:carCSVFileInputBean.class.getDeclaredFields()){
fileHeaderFields.add(f.getName());
}
}
公共静态列表getCSVInputList(InputStream InputStream){
CSVReader reader=null;
列表csvList=null;
carCSVFileInputBean inputRecord=null;
字符串[]头=null;
字符串[]行=null;
试一试{
reader=新的CSVReader(新的InputStreamReader(inputStream,UTF8CHARSET));
csvList=newarraylist();
header=reader.readNext();
布尔isEmptyLine=true;
while((row=reader.readNext())!=null){
isEmptyLine=真;
如果(!(row.length==1&&StringUtils.isBlank(row[0])){//不是空行,甚至不包含','
inputRecord=新的carCSVFileInputBean();
isEmptyLine=populateFields(输入记录、标题、行);
if(!isEmptyLine)
csvList.add(输入记录);
}
}
}捕获(IOE异常){
debug(“访问carCSVFileInputBean时发生IOException:”+e);
返回null;
}捕获(非法访问例外e){
log.debug(“访问carCSVFileInputBean时出现IllegaAccess异常:”+e);
返回null;
}捕获(调用TargetException e){
调试(“复制carCSVFileInputBean属性时调用TargetException:”+e);
返回null;
}捕获(例外e){
log.debug(“解析CSV文件时出现异常:”+e);
返回null;
}最后{
试一试{
if(读卡器!=null)
reader.close();
}捕获(ioe异常ioe){}
}
返回csvList;
}
受保护的静态布尔populateFields(carCSVFileInputBean inputRecord,字符串[]头,字符串[]行)抛出IllegaAccessException,InvocationTargetException{
布尔isEmptyLine=true;
for(int i=0;i
我找到了解决方案-csv文件中的标题应以小写字母开头。您能否提供一个可能出现错误的示例输入和输出。CarCSVFileInputBean
类被声明,但CarCSVFileInputBean
在carCSVUtil
类中使用。我找到了解决方案-csv文件中的标题应以小写字母开头。