原来的dbf导入解析是按照列来导入的,后来发现这样极不方便,列有改动那就得改代码,改成按照列名来读取
public static <T> List<T> formatDBFCommonList(String filePath,Function<Map<String,Object>, T> func){ List<T> list = Lists.newArrayList(); DBFReader reader; try { reader = new DBFReader(new FileInputStream(filePath)); reader.setCharactersetName("GBK"); String[] fieldNames = new String[reader.getFieldCount()]; for(int i=0;i<reader.getFieldCount();i++) { String fieldName = reader.getField(i).getName().trim(); fieldNames[i] = fieldName; } Object[] rowValues; int row =0; while ((rowValues = reader.nextRecord()) != null) { Map<String,Object> recordMap = Maps.newHashMap(); //第几行 recordMap.put("row",row++); for (int i = 0; i < rowValues.length; i++) { Object val = rowValues[i]; if(val instanceof String) { String str = val.toString().trim(); recordMap.put(fieldNames[i], str); }else { recordMap.put(fieldNames[i], val); } } list.add(func.apply(recordMap)); } } catch (DBFException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return list; }
List<Student> list= null; try { list = hsDbfUtil.formatDBFCommonList(filePath,(Map<String,Object> map)-> { Student student = new Student(); String XH= (String) map.get("XH"); String XB= (String) map.get("XB");
return student; }); } catch (Exception e) { e.printStackTrace(); } return list;