在项目开发过程中,有时需要将其中用到的变量值在一个文件中统一管理,首先我选到了config.properties文件;下面这个代码是用于读取其中的变量值的类:

package com.modem.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class AppProperties {
private static Properties prop = null; static {
prop = new Properties();
try {
// 1、种指定文件的位置,指定绝对路径。需要知道确切位置。
// InputStream inputStream = new
// FileInputStream("E:\\workspace\\java\\src\\config.properties");
// 2、通过类加载器来加载资源:先找到这个类,在找到这个类加载器不用在关心文件"config.properties"的具体位置
//ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// 这样也可以得到当前的ClassLoader
ClassLoader classLoader = AppProperties.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("config.properties");
prop.load(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static String getProperties(String key) {
String value = prop.getProperty(key);
if (value == null) {
value = "";
}
return value.trim();
}
}

测试类为:

package com.modem.test;

public class TestReadConfig {
public static void main(String[] args) {
String company = AppProperties.getProperties("company");
System.out.println(company);
}
}

java读取配置文件属性的更多相关文章

  1. @Value()读取配置文件属性,读出值为null的问题

    一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{ @Value("${jw ...

  2. java读取配置到Hash表里

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...

  3. Java读取ini配置

    本文转载地址:       http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 不够通用,呵呵. 读取ini的配置的格式如下 ...

  4. 部分转 Java读取ini配置

    转自: http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 读取ini的配置的格式如下: [section1] key1=v ...

  5. [Java.Web][Servlet]读取配置

    private ServletConfig config; public void doGet(HttpServletRequest request, HttpServletResponse resp ...

  6. Spring Boot 2.3 新特配置文件属性跟踪

    背景 当我们使用 spring boot 在多环境打包,配置属性在不同环境的值不同,如下: spring: profiles: active: @project.profile@ #根据maven 动 ...

  7. SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入

    全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...

  8. java Properties 配置信息类

    Properties(配置信息类):主要用于生产配置文件和读取配置文件信息. ----> 是一个集合类 继承HashTable 存值是以键-值的方式. package com.beiwo.io; ...

  9. Java读取Level-1行情dbf文件极致优化(3)

    最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...

随机推荐

  1. 【Python】解析Python中的迭代器

    目录结构: contents structure [-] Iterator VS Iterable Itertools 模块 生成器(Generator) 在开始文章之前,先贴上一张Iterable. ...

  2. (转)Loadrunner教程--常用操做流程

    1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...

  3. NVM 安装注意

    windows 系统下尽量使用安装版本,选择安装路径时,路径中不能带有空格,否则无法使用 nvm use xx.xx.xx

  4. centos6.10环境下启动多个redis实例

    # 启动redis端口6379的配置 [root@newcms:/usr/local/nginx/conf]# /etc/redis.conf daemonize yes pidfile /usr/l ...

  5. selenium===使用docker搭建selenium分布式测试环境

    准备: #请在此之前先了解,selenium grid :参考:selenium-grid ,下载地址,win-本地部署过程 >>>环境准备: Linux操作系统 >>& ...

  6. Flask 学习(二)jinja2模板介绍

    控制语句和表达式 举例 Flask Python代码 from flask import Flask, render_template, redirect, request app = Flask(_ ...

  7. iOS - 在xib中UILabel文字如何换行

    在换行的位置按住Option + Enter键即可换行

  8. c++11多线程记录2:线程管理

    线程没有调用join和detach thread对象必须调用join或者detach,否则程序会终止 例如: void func() { std::cout << "hello, ...

  9. expect——通过编写自动化脚本实现信息交互(整理)

    本文简要介绍了expect工具语言的功能.用法,并以实例来具体说明 expect是什么 Expect是一个免费的编程工具语言,用来完成通信过程中的交互式任务,而无需人的干预. 通过shell虽然可以实 ...

  10. Ubuntu中shell脚本无法使用source命令的原因与解决方法

    本文简要描述了在ubuntu系统下无法使用source命令的原因,及对应的两种解决方法,并在附录中引用一篇文章来详细解释source命令的用法 问题: 由于在交叉编译时,需要在当前shell内执行so ...