Analyzing how reflective the object type and the type of conversion to achieve strong

List static public <Object> readCsvIntoVO (<?> String readPath, Class clazz) { 
List <Object> = Result new new the ArrayList <> ();
Field, [] = clazz.getDeclaredFields Fields ();


the try {
// # division to
CsvReader = new new CSVReader CSVReader (readPath, ',', Charset.forName ( "UTF-. 8"));
csvReader.readHeaders (); // skip the header if the header if required, do not write phrase.
String [] head = csvReader.getHeaders () ; // Get header
the while (csvReader.readRecord ())
{
Object Record clazz.newInstance = ();
the Map <String, String> = new new the HashMap Map <String, String> ( );
for (String S:

if (name.charAt(0) == '\uFEFF') {
name = name.substring(1);
}
map.put(name,csvReader.get(s));
System.out.println(s + ":" + csvReader.get(s));
// String name = s.trim();
// for (Field field : fields) {
// field.setAccessible(true);
//
// if (name.equals(field.getName())) {
// Method method = clazz.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), field.getType());
// method.invoke(record, StringUtils.get(field.getType(),csvReader.get(s)));
// break;
// }
// }


// Field f = record.getClass().getDeclaredField(s);
// f.setAccessible(true);
// f.set(record, csvReader.get(s));
}
BeanUtils.populate(record, map);
result.add(record);
}
csvReader.close();
} catch (FileNotFoundException e) {
throw new RuntimeException("文件未找到");
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
return result;
}

Guess you like

Origin www.cnblogs.com/neaos/p/11308220.html