package test;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import entity.HelpTopicId;
public class TestMap
{
public static void restoreProp(Object bean, Map<String, Object> map)
{
BeanInfo beanInfo;
try
{
beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : descriptors)
{
// 属性名字
String fieldName = propertyDescriptor.getName().toUpperCase();
String fieldType = propertyDescriptor.getPropertyType().getName();
if (map.containsKey(fieldName))
{
Method setter = propertyDescriptor.getWriteMethod();
if("java.lang.Short".equals(fieldType))
{
setter.invoke(bean, new Object[] { (Short.parseShort(map.get(fieldName)+"")) });
continue;
}
if("java.lang.Integer".equals(fieldType))
{
setter.invoke(bean, new Object[] { (Integer.parseInt(map.get(fieldName)+"")) });
continue;
}
setter.invoke(bean, new Object[] { map.get(fieldName) });
}
}
} catch (IntrospectionException e)
{
e.printStackTrace();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
public static List<Map<String, Object>> readCSV(File file)
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String msg = "";
int i = 0;
while ((msg = br.readLine()) != null)
{
String[] line = msg.split(",");
if (line == null || "".equals(line[0]))
{
break;
}
Map<String, Object> map = new HashMap<String, Object>();
if (i == 0)
{
for (int j = 0; j < line.length; j++)
{
line[j] = line[j].replaceAll("\"", "").replaceAll("_", "").toUpperCase();
if(j==0)
{
line[j]=line[j].substring(line[j].indexOf("H"), line[j].length());
}
map.put(j + "", line[j]);
}
list.add(map);
i++;
continue;
}
for (int k = 0; k < line.length; k++)
{
line[k] = line[k].replaceAll("\"", "");
map.put(list.get(0).get(k + "")+"", line[k]);
}
list.add(map);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return list;
}
public static HashMap<String, Object> backupProp(Object bean)
{
HashMap<String, Object> result = new HashMap<String, Object>();
try
{
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors = beanInfo
.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : descriptors)
{
String fieldName = propertyDescriptor.getName();
Method getter = propertyDescriptor.getReadMethod();
Object fieldValue = getter.invoke(bean, new Object[] {});
System.out.println("bean 的属性名字:" + fieldName + " 属性值:"
+ fieldValue);
if (!fieldName.equalsIgnoreCase("class"))
{
result.put(fieldName, fieldValue);
}
}
} catch (IntrospectionException e)
{
e.printStackTrace();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (InvocationTargetException e)
{
e.printStackTrace();
}
return result;
}
public static void main(String[] args)
{
// BookAuthor author = new BookAuthor();
// author.setName("lx");
//
// Book book = new Book();
// book.setBookName("java");
// book.setPrice(22);
// book.setAuthor(author);
//
// backupProp(book);
List<Map<String, Object>> list = readCSV(new File("F:\\Documents and Settings\\Administrator\\桌面\\help_topic.csv"));
for(int i=1;i<list.size();i++)
{
HelpTopicId bean = new HelpTopicId();
restoreProp(bean, list.get(i));
System.out.println(bean.toString());
}
}
}