使用Spring读取package里面的Proerites文件
Properties detailProps = loadProperties("com/test.properties");
protected Properties loadProperties(String resource) throws IOException {Properties props = new Properties();InputStream is = null;try {is = ResourceHelper.getClassPathResourceAsInputStream(resource);if (is != null) {propertiesPersister.load(props, new InputStreamReader(is, "UTF-8"));}} finally {IOUtils.closeQuietly(is);}return props;}
package com.tools;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.util.ResourceUtils;/** * 资源操作工具类 * * @author ourenyang * */public class ResourceHelper {/** * 在指定类所在包下查找指定的资源文件,并转换成InputStream * * @param file * @param clazz * @return * @throws IOException */public static InputStream getResourceInPackage(String file, Class<?> clazz) throws IOException {StringBuilder path = new StringBuilder(clazz.getPackage().getName().replace('.', '/'));if (file.charAt(0) != '/') {path.append('/');}path.append(file);Resource res = new ClassPathResource(path.toString());return res.getInputStream();}/** * 在指定类所在包下查找指定的资源文件,并转换成InputStream * * @param file * @param clazz * @return * @throws IOException */public static Reader getResourceInPackageAsReader(String file, Class<?> clazz) throws IOException {StringBuilder path = new StringBuilder(clazz.getPackage().getName().replace('.', '/'));if (file.charAt(0) != '/') {path.append('/');}path.append(file);Resource res = new ClassPathResource(path.toString());return new InputStreamReader(res.getInputStream());}/** * 从Class路径中获取指定资源文件,并转换成InputStream * * @param path * @return * @throws IOException */public static InputStream getClassPathResourceAsInputStream(String path) throws IOException {Resource res = new ClassPathResource(path);return res.getInputStream();}/** * 获取指定资源文件,并转换成InputStream,资源文件名规则见Spring * * @param path * @return * @throws IOException */public static InputStream getResourceAsInputStream(String path) throws IOException {File file = ResourceUtils.getFile(path);return new FileInputStream(file);}/** * 从Class路径中获取指定资源文件,并转换成Reader * * @param path * @return * @throws IOException */public static Reader getClassPathResourceAsReader(String path) throws IOException {Resource res = new ClassPathResource(path);InputStream is = res.getInputStream();return new InputStreamReader(is);}/** * 获取指定资源文件,并转换成Reader,资源文件名规则见Spring * * @param path * @return * @throws IOException */public static Reader getResourceAsReader(String path) throws IOException {File file = ResourceUtils.getFile(path);return new FileReader(file);}}