在项目开发过程中,有时需要将其中用到的变量值在一个文件中统一管理,首先我选到了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. 批量实现ssh免密登录

    本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...

  2. linux tomcat 文件切割

    修改bin目录下catalina.sh if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE&quo ...

  3. nginx https 转发

    add_header Content-Security-Policy upgrade-insecure-requests;

  4. 【转载】 tf.cond() ----------------------(tensorflow 条件判断语句 if.......else....... )

    原文地址: https://cloud.tencent.com/developer/article/1486441 ------------------------------------------ ...

  5. Python3基础 print %xX 十六进制大小写

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  6. Html表格和表头文字不换行

    [本文出自天外归云的博客园] 希望表头中的文字和表格中的文字不换行,只需要在th和td标签加上: nowrap="nowrap"

  7. 二代身份Zheng复印(手机正反面拍照生成1:1复印件图像)

    下载地址 2017年7月28日 1.支持文字水印:2.优化程序,修复Bug:3.升级版本为1.1. 2017年7月17日1.二代身份*证正面(白色按钮).反面(蓝色按钮)拍摄后,退出APP自动生成A4 ...

  8. full text search

    definition https://www.techopedia.com/definition/17113/full-text-search A full-text search is a comp ...

  9. Java中遍历ConcurrentHashMap的四种方式

    //方式一:在for-each循环中使用entries来遍历 System.out.println("方式一:在for-each循环中使用entries来遍历"); for(Map ...

  10. 伪造他人账号信息提交数据问题(一般接口都会去校验身份和身份id是否一致)

    某APP客户端可以劫持任意账号 双师: 上课的学生上报学生上课状态,请求中带学生自己的id,当切换成其他学生的id ,接口报错,应该会校验token 和 提交的学生id 是否一致