在项目开发过程中,有时需要将其中用到的变量值在一个文件中统一管理,首先我选到了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. Ubuntu 上编译opencv_contrib模块for Android

    https://blog.csdn.net/ipfpm/article/details/81132144 [ubuntu]Ubuntu中Android SDK下载跟配置 android24的版本 (1 ...

  2. 请关注订阅号,获取demo,学术讨论

  3. ABAP基础篇2 数据类型

    基本数据类型列表: 1.长度可变的内置类型(String.XString)1)string类型 在ABAP程序中,string类型是长度无限的字符型字段,可以和CHAR ,D,T ,I,N (F和P未 ...

  4. Redis面试大全

    1. 什么是Redis Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...

  5. 做JAVA的需要了解的框架

    spring netty Elasticsearch Eureka Hystrix 接口的依赖性管理 Zuul Config Bus ActiveMQ redis zookper quartz had ...

  6. Visual Studio + Qt:GetVarsFromMakefile任务意外失败

    问题: IntelliSense报告找不到头文件: 编译时报告GetVarsFromMakefile任务意外失败. 解决: 删除从Visual Studio装的Qt插件: 从Qt官网下载最新的插件:h ...

  7. 本地win下JConsole监控远程linux下的JVM

    环境:服务器端: Linux + jdk1.7.0_75 + tomcat 7本地: Win + jdk1.7.0_55 一.修改/etc/hosts文件 hostname -i 如果显示127.0. ...

  8. python语法入门之变量

    目录 一.变量 1.1 什么是变量 1.2 怎么使用变量 1.3 变量名的命名规范 1.4 变量名的命名风格 1.5 变量的三大特征 2.常量 一.变量 1.1 什么是变量 # 变量就是可以变化的量, ...

  9. myssl.com SSL 检测

    配置正确了,就正常了. 与证书关系不大.

  10. 深入Vue响应式原理

    深入Vue.js响应式原理 一.创建一个Vue应用 new Vue({ data() { return { name: 'yjh', }; }, router, store, render: h =& ...