1.配置文件test.properties:

test_123=admin

注:value 可用单引号,双引号,不用引号修饰

2.工具类PropertiesUtil:

package com.......test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {

    private static final String PROP_FILE = "test.properties";

    private static Properties properties;

    static {
properties = new Properties();
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROP_FILE);
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));//解决读取properties文件中产生中文乱码的问题
assert (is != null);
properties.load(bf); } catch (IOException e) {
throw new RuntimeException("读取" + PROP_FILE + "配置文件异常", e);
}
} public static String getValue(String key) {
return properties.getProperty(key);
}
}

3.测试

    @Test
public void test() {
String keyWord = "test_123";
String value = PropertiesUtil.getValue(keyWord);
System.out.println("value = " + value);
}

properties配置文件读取的更多相关文章

  1. @Value和@PropertySource实现*.properties配置文件读取过程和实现原理

    @Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1       配置使用步骤 (1)右击resource目录添加*.prooerties配置文件

  2. properties配置文件读取操作总结【java笔记】

    声明:本文所有例子中的 properties 文件均放在 src 目录下,ecclipse 软件自动增加 一.基本概念 1.1  properties文件,存储格式 键=值. properties文件 ...

  3. Java中Properties配置文件读取

    以下实践的是Properties配置文件的基本操作方法.像spring使用xml做依赖注入时,这个配置文件起到非常实用的作用. 一.格式规范 参考wiki百科的格式简介:https://zh.wiki ...

  4. Spring Boot的properties配置文件读取

    我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...

  5. 读取.properties配置文件

    方法1 public  class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...

  6. 单例模式读取properties配置文件中的信息

    public class ConfigManager {    private static ConfigManager config = null;    //创建Properties文件  读取配 ...

  7. java读取properties配置文件总结

    java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...

  8. properties配置文件的读取和写入

    /** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...

  9. java读取properties配置文件方法(一)

    为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...

随机推荐

  1. java面试题之BeanFactory和FactoryBean的区别

    BeanFactory是个Factory,也就是IOC容器或对象工厂:FactoryBean是个Bean.在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器)来进行管理的. ...

  2. openxml的视频教程

    http://msdnwebcast.net/webcast/0/1980/#1032360142 最近发现的一个openxml的视频教程

  3. Redis Cluster 集群的实现和管理

    系统环境 CentOS 7 集群规划 在一台物理机(实际部署应当分散到多个物理机上),创建6个Redis节点,其中3个主节点.3个从节点. 节点表: IP 端口 主从 路径 192.168.1.21 ...

  4. html5(拖拽1)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. (5)html音频视频

    音频 1.互联网常用的音频格式 ogg 有损的音频压缩技术 免费的开源音频格式 它可以在相对较低的数据速率下实现比MP3更好的音质 mp3 有损的音频压缩技术 想使用MP3格式发布自己的作品,需要付给 ...

  6. Failed to check the status of the service报错解决

    报这个错误是因为我的application_context.service.xml 文件里的的dubbo声明暴露口时的ref属性写错了. <dubbo:service interface=&qu ...

  7. 结构体和类中属性定义需要static地方

    private function Readxxx:Integer;static; public class property XXX:Integer read ReadXXx; Txxx =recor ...

  8. 《SQL Server 监控和诊断》

    http://jimshu.blog.51cto.com/ http://www.mssqlmct.cn/

  9. Examples osgparticleshader例子学习

    Examples osgparticleshader  粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...

  10. PriorityQueue ,ArrayList , 数组排序

    static class E implements Comparable<E>{ int x ; int y ; int state ; int money ; public E(int ...