【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 ...
随机推荐
- upstream timed out (110: Connection timed out) while reading response header from upstream
Nginx报错日志有如下内容: upstream timed out (110: Connection timed out) while reading response header from up ...
- [LeetCode&Python] Problem 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- C#线程、前后台线程
C#线程.前后台线程 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新 ...
- CF444(Div. 1简单题解)
A .DZY Loves Physics 题意:给定带点权和边权的无向图,现在让你选一些点,使得 点权和/被选点对间的边权和 最大. 思路:不难证明,选择边和对应的两点是最优的. #include&l ...
- day01计算机组成与操作系统
1.什么是编程语言编程语言是程序员与计算机之间的沟通介质 2.什么是编程编程的过程就是程序员通过某种语言将命令给到计算机并让计算机表达出来 修改后:编程就是程序员按照某种语法规则将自己想让计算机做的事 ...
- 为何linux(包括mac系统)执行指令要加上 ./ ??
比如,现在要在$HIVE_HOME/bin下执行hive指令来启动hive,则该指令的执行顺序如下所示: 1 先找PATH路径 1.1 如果PATH路径下配置了$HIVE_HOME/bin,无论PAT ...
- hdu3974 Assign the task dfs序+线段树
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- lsusb
1.lsusb查看系统的USB设备 $ lsusb Bus Device : ID : Kingston Technology Bus Device : ID 1d6b: Linux Foundati ...
- 你不知道的JavaScript(上卷) (Kyle Simpson 著)
第一部分 作用域和闭包 第1章 作用域是什么 (已看) 1.1 编译原理 1.2 理解作用域 1.2.1 演员表 1.2.2 对话 1.2.3 编译器有话说 1.2.4 引擎和作用域的对话 1.2.5 ...
- 从一到无穷大:科学中的事实和臆测 (G. 伽莫夫 著)
第一部分 做做数字游戏 第一章 大数 (已看) 第二章 自然数和人工数 (已看) 第二部分 空间,时间与爱因斯坦 第三章 空间的不寻常的性质 (已看) 第四章 四维世界 (已看) 第五章 时间和空间的 ...