Java tutorial
/** * Copyright (c) 2005-2010 wwinsoft.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: PropertiesUtils.java 1211 2010-09-10 16:20:45Z calvinxiu $ */ package com.wwinsoft.modules.utils; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.commons.beanutils.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.util.DefaultPropertiesPersister; import org.springframework.util.PropertiesPersister; import org.springframework.util.ReflectionUtils; /** * Properties Util. * * @author calvin */ public class PropertiesUtils { private static final String DEFAULT_ENCODING = "UTF-8"; private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class); private static PropertiesPersister propertiesPersister = new DefaultPropertiesPersister(); private static ResourceLoader resourceLoader = new DefaultResourceLoader(); /** * properties, ???. * Spring Resource?, ?UTF-8. * * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer */ public static Properties loadProperties(String... resourcesPaths) throws IOException { Properties props = new Properties(); for (String location : resourcesPaths) { logger.debug("Loading properties file from:" + location); InputStream is = null; try { Resource resource = resourceLoader.getResource(location); is = resource.getInputStream(); propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING)); } catch (IOException ex) { logger.info("Could not load properties from classpath:" + location + ": " + ex.getMessage()); } finally { if (is != null) { is.close(); } } } return props; } public static Map toParameterMap(Object parameter) { if (parameter instanceof Map) { return (Map) parameter; } else { try { return PropertyUtils.describe(parameter); } catch (Exception e) { ReflectionUtils.handleReflectionException(e); return new HashMap(); } } } }