java读取配置文件属性
在项目开发过程中,有时需要将其中用到的变量值在一个文件中统一管理,首先我选到了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读取配置文件属性的更多相关文章
- @Value()读取配置文件属性,读出值为null的问题
一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{ @Value("${jw ...
- java读取配置到Hash表里
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...
- Java读取ini配置
本文转载地址: http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 不够通用,呵呵. 读取ini的配置的格式如下 ...
- 部分转 Java读取ini配置
转自: http://www.cnblogs.com/Jermaine/archive/2010/10/24/1859673.html 读取ini的配置的格式如下: [section1] key1=v ...
- [Java.Web][Servlet]读取配置
private ServletConfig config; public void doGet(HttpServletRequest request, HttpServletResponse resp ...
- Spring Boot 2.3 新特配置文件属性跟踪
背景 当我们使用 spring boot 在多环境打包,配置属性在不同环境的值不同,如下: spring: profiles: active: @project.profile@ #根据maven 动 ...
- SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入
全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...
- java Properties 配置信息类
Properties(配置信息类):主要用于生产配置文件和读取配置文件信息. ----> 是一个集合类 继承HashTable 存值是以键-值的方式. package com.beiwo.io; ...
- Java读取Level-1行情dbf文件极致优化(3)
最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...
随机推荐
- Embed MP4 in HTML using flash-player(html5 video player)
https://stackoverflow.com/questions/1000851/embed-mp4-in-html-using-flash-player ******************* ...
- coding git push 403 时
直接修改 项目目录下的 .git/config url url = https://coding用户名:coding密码@git.coding.net/coding账号/coding项目名称.gi ...
- 常用的js片段
1.检查是否为微信浏览器 function isWxBrowser() { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/Micr ...
- c#实现定时任务(Timer)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MySQL Community Server 8.0.16
1 首先 我们需要先下载一个 Mysql 点击这个网址进入 Mysql 的官网的下载地址: https://dev.mysql.com/downloads/mysql/ 首先 根据你的电脑的操作系统选 ...
- MQTT的Res接口发布消息
MQTT(这里采用的V2版本)发布消息的常见方法: 1.通过MQTT客户端连接MQTT服务器,建立长连接,通过接口发布消息 最常见的客户端: <dependency> <groupI ...
- Python实现字典树
字典树,又称单词查找树,Trie 树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串 ...
- C++中const限定符
const基础 C++中的const,用于定义一个常量,这个常量的值不能被修改.因为const对象一旦创建就不能修改,所以const对象必须初始化.const常量特征仅仅在执行改变其本身的操作时才会发 ...
- 如何获取文件夹下所有文件名称(windows)
1. win+R -> cmd 打开dos命令窗口 2. 打开需获取文件名的位置 3. 获取名称 命令格式:dir /b >> 文件目标盘符:\文件夹位置(可省略)\目标名称.目标后 ...
- 20.Python略有小成(面向对象Ⅱ)
Python(面向对象Ⅱ) 一.类的空间问题 何处可以添加对象属性 class A: def __init__(self,name): self.name = name def func(self,s ...