Bean自动赋值

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());
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值