获取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文件的更多相关文章

  1. Spring3.x 获取properties资源文件的值

    Spring3.x 获取properties资源文件的值有两种方式:  第一种:使用<context:property-placeholder />标签  <context:prop ...

  2. java工具类获取properties文件的配置

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  3. 获取properties文件的内容

    获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...

  4. Java Bean 获取properties文件的读取

    实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...

  5. Struts2学习:Action获取properties文件的值

    配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...

  6. spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别

    spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...

  7. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  8. winform中获取Properties窗口的值.

    我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样. using System; using System.Collections.Generic ...

  9. @Value 注解获取properties值

    转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...

随机推荐

  1. Linux命令学习之路——文档权限管理:chmod

    使用权限:所有角色 使用方式:chmod [ -cfvR ] [ --help ] [ --version ] mode file... 作用:该命令用于在Linux中管理和变更角色对文档的存取权限 ...

  2. HDU - 4454: Stealing a Cake (圆上三分)

    pro:给定一个蛋糕,一个矩阵房子,一只蚂蚁.最开始三者两两相离,问蚂蚁触摸到蛋糕后再触摸矩阵的最短距离.结果保留两位小数,坐标的绝对值<1e4: sol:由于坐标不大,而且精度要求不高,不难想 ...

  3. 20155219 2016-2017-2 《Java程序设计》第2周学习总结

    20155219 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 类型:整数,字节(需要逐字节处理数据时,如图像处理,编码处理等,会使用byte类型),浮点数 ...

  4. PTA——猜数字游戏

    PTA 7-24 猜数字游戏 #include <stdio.h> int main() { int num, times; scanf("%d %d", &n ...

  5. java-Integer类

    1.为什么会有基本类型包装类 * 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据. 2.常用操作 * 常用的操作之一:用于基本数据类型与字符串之间的转换. * Intege ...

  6. Linux,IDS入侵防御系统

    https://www.comparitech.com/net-admin/network-intrusion-detection-tools/11 2018年的顶级入侵检测工具 https://op ...

  7. emacs安装

    1.我选用的是Ubuntu16.04. 2.Ubuntu安装好之后不能直接sudo apt-get install emacs,因为Ubuntu的源默认是emacs24,最好是用最新的emacs25, ...

  8. 高性能kv存储之Redis、Redis Cluster、Pika:如何应对4000亿的日访问量?

    一.背景介绍 随着360公司业务发展,业务使用kv存储的需求越来越大.为了应对kv存储需求爆发式的增长和多使用场景的需求,360web平台部致力于打造一个全方位,适用于多场景需求的kv解决方案.目前, ...

  9. fcntl获取和修改文件打开状态标志

    [root@bogon code]# cat b.c #include<stdio.h> #include<error.h> #include<unistd.h> ...

  10. day 49 html 学习 css 学习

    画圆 <style> div{width: 100px; height:100px; border: solid red 3px; /*当弧度为.圆角半径为30时 得到的图像*/ bord ...