1、 打开文件的方式有:
InputStream is = ResourcePropertiesUtils.class.getClassLoader().getResourceAsStream(name);
ClassPathResource classPathResource = new ClassPathResource(name);
try {
if(classPathResource.exists()){
is = classPathResource.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fis = null;
File file = new File(name);
if(file.exists()){
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2、读取配置的方式有
1、
public static Properties getProperties(InputStream is){
Properties properties = null;
try{
properties = new Properties();
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
properties.load(bf);
is.close();
}catch (Exception ex){
ex.printStackTrace();
}
return properties;
}
配置文件读取顺序
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
代码
public class ResourcePropertiesUtils {
static Logger log = LoggerFactory.getLogger(ResourcePropertiesUtils.class);
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/,";
public static Properties getProperties(InputStream is){
Properties properties = null;
try{
properties = new Properties();
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
properties.load(bf);
is.close();
}catch (Exception ex){
ex.printStackTrace();
}
return properties;
}
/**
* 根据文件路径获取流
* @return
*/
public static Properties getProperties(String name){
String[] files = DEFAULT_SEARCH_LOCATIONS.split(",");
Properties properties = null;
for (String file:files
) {
if(file!=null){
String fileName = file.concat(name);
properties = getStramProperties(fileName);
if(properties!=null && !CollectionUtils.isEmpty(properties.keySet())){
log.info("读取参数:"+fileName);
}
}
}
return properties;
}
public static Properties getStramProperties(String name){
InputStream is = ResourcePropertiesUtils.class.getClassLoader().getResourceAsStream(name);
Properties properties = new Properties();
if(is!=null){
log.info("classloader 获取参数:"+name);
properties = getProperties(is);
}
ClassPathResource classPathResource = new ClassPathResource(name);
try {
if(classPathResource.exists()){
is = classPathResource.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
if(is!=null){
log.info("classpath 获取参数:"+name);
Properties tmp = getProperties(is);
if(tmp!=null){
for (Object key:tmp.keySet()
) {
properties.put(key,tmp.get(key));
}
}
}
File file = new File(name);
if(file.exists()){
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(fis!=null){
log.info("FileInputStream 获取参数:"+name);
Properties tmp = getProperties(is);
if(tmp!=null){
for (Object key:tmp.keySet()
) {
properties.put(key,tmp.get(key));
}
}
}
}
return properties;
}
}
测试
@RestController
@RequestMapping("/property")
public class PropertiesController {
@RequestMapping("/get")
public Result getProperties(String path){
Properties properties = ResourcePropertiesUtils.getStramProperties(path);
Map<Object,Object> map = new HashMap<>();
for (Object obj: properties.keySet()
) {
map.put(obj,properties.get(obj));
}
return Result.success(map);
}
@RequestMapping("/getPath")
public Result getPathProperties(String path){
Properties properties = ResourcePropertiesUtils.getProperties(path);
Map<Object,Object> map = new HashMap<>();
for (Object obj: properties.keySet()
) {
map.put(obj,properties.get(obj));
}
return Result.success(map);
}
}
执行结果
1、debug执行
127.0.0.1:8080/property/getPath?path=application.properties
执行情况 找不到
2、直接运行
目录结构
192.168.128.15:8080/property/get?path=application.properties
192.168.128.15:8080/property/getPath?path=application.properties