经常要读取txt或者excel到数据库
以前都是读取后然后转成list,然后set到表对象(hibernate映射的表对象),
然后再session.save(entity),
如果文件中的title很多的话,就太麻烦了。
现在用java 反射,可以轻松解决
ClubWinterActivity是表对象,new String[]{}中是txt中的title,就是说取这些值,放入表对象的这些字段:
- List<ClubWinterActivity> list = (List<ClubWinterActivity>) ParserTxtFileUtils
- .getListByFile(file, ClubWinterActivity.class, new String[] {
- "cwatype", "cwatimes", "cwacustomername", "cwavin","flag"});
List<ClubWinterActivity> list = (List<ClubWinterActivity>) ParserTxtFileUtils
.getListByFile(file, ClubWinterActivity.class, new String[] {
"cwatype", "cwatimes", "cwacustomername", "cwavin","flag"});
读取每行txt,放入getObject()中
- public static List<?> getListByFile(File file, Class<?> clazz, String[] fieldNames) throws Exception {
- List<Object> list = new ArrayList<Object>();
- InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GB2312");
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- while (null != (line = br.readLine())) {
- Object o = getObject(line, clazz, fieldNames);
- if(null != o)
- list.add(o);
- }
- return list;
- }
public static List<?> getListByFile(File file, Class<?> clazz, String[] fieldNames) throws Exception {
List<Object> list = new ArrayList<Object>();
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GB2312");
BufferedReader br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
Object o = getObject(line, clazz, fieldNames);
if(null != o)
list.add(o);
}
return list;
}
- private static Object getObject(String line, Class<?> clazz,
- String[] fieldNames) throws Exception {
- Object o = null;
- if (!"".equals(line)) {
- o = clazz.newInstance();
- String[] values = line.split(",");
- setObjectFieldsValue(o, values, fieldNames);
- }
- return o;
- }
private static Object getObject(String line, Class<?> clazz,
String[] fieldNames) throws Exception {
Object o = null;
if (!"".equals(line)) {
o = clazz.newInstance();
String[] values = line.split(",");
setObjectFieldsValue(o, values, fieldNames);
}
return o;
}
把值set到o中
- private static void setObjectFieldsValue(Object o, String[] values,
- String[] fieldNames) throws Exception {
- if (null != values && values.length > -1) {
- for (int i = 0; i < fieldNames.length; i++) {
- Field field = o.getClass().getDeclaredField(fieldNames[i]);
- field.setAccessible(true);
- String typeName = field.getType().getSimpleName();
- if ("Long".equals(typeName) || "long".equals(typeName))
- field.set(o, Long.parseLong(values[i]));
- else if ("Date".equals(typeName))
- field.set(o, DateUtils.getDateByFormat(values[i],
- Constants.DATEFORMAT1));
- else if ("Integer".equals(typeName) || "int".equals(typeName))
- field.set(o, Integer.parseInt(values[i]));
- else if ("Double".equals(typeName) || "double".equals(typeName))
- field.set(o, Double.parseDouble(values[i]));
- else if ("Float".equals(typeName) || "float".equals(typeName))
- field.set(o, Float.parseFloat(values[i]));
- else if ("Boolean".equals(typeName)
- || "boolean".equals(typeName))
- field.set(o, Boolean.parseBoolean(values[i]));
- else
- field.set(o, values[i]);
- }
- }
- }