使用Spring读取package里面的Proerites文件

本文介绍了一个Java工具类ResourceHelper,该类提供了多种方法来从不同的位置加载资源文件,例如从类路径或者指定类所在的包下加载资源文件为InputStream或Reader,并展示了如何使用这些方法来加载属性文件。
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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值