【Properties】获取Properties文件
获取Properties文件
package com.chinamobile.epic.tako.v2.query.commons;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
public class PropertiesUtilsBase {
/**
* 获取属性文件
* @param path example: c:\\my.properties
* @return
*/
public static Properties loadFromFileSystem(String path) {
Properties props = new Properties();
try {
File file = new File(path);
InputStream in = new BufferedInputStream(new FileInputStream(file));
//解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
props.load(bf);
in.close();
} catch (Exception e) {
return null;
}
return props;
}
/**
* 从classpath中获取属性文件
*
* @param path graphite/graphiteTargets.properties
* @return
*/
public static Properties loadFromClassPath(String path) {
Resource resource = new ClassPathResource(path);
Properties prop = null;
try {
prop = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
public static Map<String, String> asMap(Properties properties) {
if (properties == null) {
return null;
}
Map<String, String> entryMap = new ConcurrentHashMap<String, String>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
entryMap.put(key, value);
}
return entryMap;
}
/**
* 显示所有key,value
*
* @param properties
*/
public static void showKeysAndValues(Properties properties) {
if (properties == null) {
System.out.println("properties == null");
return;
}
Iterator<Map.Entry<Object, Object>> it = properties.entrySet().iterator();
System.out.println("======下面将显示所有<key,value>值---方式1============");
while (it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
Object key = entry.getKey().toString();
Object value = entry.getValue();
System.out.println("<" + key + "," + value + ">");
}
}
}
使用@Bean方式获取Properties
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
【Properties】获取Properties文件的更多相关文章
- Spring3.x 获取properties资源文件的值
Spring3.x 获取properties资源文件的值有两种方式: 第一种:使用<context:property-placeholder />标签 <context:prop ...
- java工具类获取properties文件的配置
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- Java Bean 获取properties文件的读取
实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...
- Struts2学习:Action获取properties文件的值
配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
- winform中获取Properties窗口的值.
我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样. using System; using System.Collections.Generic ...
- @Value 注解获取properties值
转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...
随机推荐
- Linux 查看CPU信息、机器型号等硬件信息[转]
查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- Java IntelliJ IDEA 不能显示项目里的文件结构
方法一: 关闭IDEA, 然后删除项目文件夹下的.idea文件夹 重新用IDEA工具打开项目 方法二: 菜单:File -> Invalidate Caches / Restart
- 20155219实践题目实现od命令
实践题目 编写MyOD.c 用myod XXX实现Linux下od -tx -tc XXX的功能 od的功能: od命令用于将指定文件内容以八进制.十进制.十六进制.浮点格式或ASCII编码字符方式显 ...
- JAVA:形参与实参
今天百度startWith函数的用法,无意中看到了形参这个称呼,因此就去了解了下形参与实参. 在传值机制中,其实就是把变量b(实参)的地址传递给了形参(也就是实参跟形参都是用的同一个地址,在传值之前形 ...
- asm.js 和 Emscripten 入门教程
http://www.ruanyifeng.com/blog/2017/09/asmjs_emscripten.html
- 一次scrapy成功停止的信息
2017-11-05 18:52:42 [scrapy.core.engine] INFO: Closing spider (finished)2017-11-05 18:52:42 [scrapy. ...
- javascript 日常
$('#code').bind('keypress', function (e) { //绑定回车处理 ) { console.log($("#code")); } }); $.a ...
- 《DSP using MATLAB》Problem 6.3
天不亮又醒了,拍了张景象,这就是黎明前的黑暗吗
- 【HAOI2014】遥感监测
独立博客被硬盘保护干掉了真不爽啊…… 原题: 外星人指的是地球以外的智慧生命.外星人长的是不是与地球上的人一样并不重要,但起码应该符合我们目前对生命基本形式的认识.比如,我们所知的任何生命都离不开液态 ...