获取Properties文件

  1. package com.chinamobile.epic.tako.v2.query.commons;
  2. import org.springframework.core.io.ClassPathResource;
  3. import org.springframework.core.io.Resource;
  4. import org.springframework.core.io.support.PropertiesLoaderUtils;
  5. import java.io.*;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Properties;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. public class PropertiesUtilsBase {
  11. /**
  12. * 获取属性文件
  13. * @param path example: c:\\my.properties
  14. * @return
  15. */
  16. public static Properties loadFromFileSystem(String path) {
  17. Properties props = new Properties();
  18. try {
  19. File file = new File(path);
  20. InputStream in = new BufferedInputStream(new FileInputStream(file));
  21. //解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
  22. BufferedReader bf = new BufferedReader(new InputStreamReader(in));
  23. props.load(bf);
  24. in.close();
  25. } catch (Exception e) {
  26. return null;
  27. }
  28. return props;
  29. }
  30. /**
  31. * 从classpath中获取属性文件
  32. *
  33. * @param path graphite/graphiteTargets.properties
  34. * @return
  35. */
  36. public static Properties loadFromClassPath(String path) {
  37. Resource resource = new ClassPathResource(path);
  38. Properties prop = null;
  39. try {
  40. prop = PropertiesLoaderUtils.loadProperties(resource);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. return prop;
  45. }
  46. public static Map<String, String> asMap(Properties properties) {
  47. if (properties == null) {
  48. return null;
  49. }
  50. Map<String, String> entryMap = new ConcurrentHashMap<String, String>();
  51. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  52. String key = entry.getKey().toString();
  53. String value = entry.getValue().toString();
  54. entryMap.put(key, value);
  55. }
  56. return entryMap;
  57. }
  58. /**
  59. * 显示所有key,value
  60. *
  61. * @param properties
  62. */
  63. public static void showKeysAndValues(Properties properties) {
  64. if (properties == null) {
  65. System.out.println("properties == null");
  66. return;
  67. }
  68. Iterator<Map.Entry<Object, Object>> it = properties.entrySet().iterator();
  69. System.out.println("======下面将显示所有<key,value>值---方式1============");
  70. while (it.hasNext()) {
  71. Map.Entry<Object, Object> entry = it.next();
  72. Object key = entry.getKey().toString();
  73. Object value = entry.getValue();
  74. System.out.println("<" + key + "," + value + ">");
  75. }
  76. }
  77. }

使用@Bean方式获取Properties

  1. @Bean
  2. public Properties quartzProperties() throws IOException {
  3. PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  4. propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
  5. propertiesFactoryBean.afterPropertiesSet();
  6. return propertiesFactoryBean.getObject();
  7. }

【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. JavaSE笔记

    this关键字 哪个对象调用方法,方法定义中的this即为该对象的引用! static关键字 使用static声名的成员变量为静态成员变量,在第一次使用的时候被初始化,static成员变量只有一份 使 ...

  2. Python 模块管理1

    Python 模块管理   导入新的模块 创建一个 calculate.py 文件 print('ok') def add(x,y): return x + y def sub(x,y): retur ...

  3. Windows 10瘦身

    Windows操作系统功能越来越强大,同时体型也越来越臃肿.安装盘没有60Gb都不敢安装.Windows10 安装最低磁盘要求20G ====瘦身基础篇,适合任何用户:(可见目录,简单迁移)1. 安装 ...

  4. Python开发指南规范

    分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Python会将 圆括号, 中括号和花括号 ...

  5. python------面向对象介绍之经典类与新式类的继承顺序

    一. 经典类与新式类的继承顺序 1 class A: def __init__(self): print("A") class B(A): def __init__(self): ...

  6. linux系统lnmp环境包搬家教程

    打包搬家apt-get install zip unzip -yyum install zip unzip -y# debian ubuntu 用apt-get,centos用yumcd /home/ ...

  7. Scala 方法与函数

    Scala 方法与函数:http://www.runoob.com/scala/scala-functions.html Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分 ...

  8. svelte 构建快速web 应用的工具

    svelte 和angular vue reat 类似,都是方便快速的创建用户界面,最大不同的地方是svelte 转换你的app 是在构建时,而不是运行时,所以好处就是不用花费太多的操作在,框架的 抽 ...

  9. zeebe docker-compose 运行(包含monitor)

    环境准备 docker-compose 文件 version: "3" services: db: image: oscarfonts/h2 container_name: zee ...

  10. JS中encodeURIComponent函数用php解码的代码

    JS中encodeURIComponent函数给中文编码后,如何用php解码?? 前提:编码前的中文可能是gbk,gb2312,utf-8等. 复制代码 代码如下: urldecode() iconv ...