properties配置文件读取操作总结【java笔记】
声明:本文所有例子中的 properties 文件均放在 src 目录下,ecclipse 软件自动增加
一、基本概念
1.1
properties文件,存储格式 键=值。
properties文件特点:
1、键值对格式
2、“ = ”等号后面,值前面,的空格,会自动忽略掉
3、值后面的空格,不会忽略
4、“ = ”等号后面的双引号,不会忽略
5、“ # ”井号后面内容,为注释,忽略
1.2 Java的 Properties 类 属性映射(property map)
是一种存储键/值对的数据结构。属性映射经常被用来存放配置信息。
它有三个特性:
1. 键和值斗志字符串
2. 键/值对可以很容易地写入文件或从文件读出
3. 用二级表存放默认值
实现属性映射的Java类被称为 Properties(Java.util.Properties),此类是Java中比较重要的类,
主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,
这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置,提高程序的可扩展性。
此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步。
构造方法:
- Properties() 创建一个无默认值的空属性列表。
- Properties( Properties defaults ) 创建一个带有指定默认值的空属性列表。
它提供了几个主要的方法:
getProperty ( String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
load ( InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
setProperty ( String key, String value) :底层调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
store ( OutputStream out, String comments):以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
clear ():清除所有装载的 键 - 值对。该方法在基类中提供。
注意:因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。
如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。
类似地,如果在“不安全”的 Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。
二、Java读取Properties文件的方法
2.1 Java虚拟机(JVM)有自己的系统配置文件(system.properties)
//获取JVM的系统属性
import java.util.Properties;
public class ReadJVM {
public static void main(String[] args) {
Properties pps = System.getProperties();
pps.list(System.out);
}
}
2.2 使用 J2SE API 读取 Properties 文件的六种方法
方法一:java.util.Properties类的 load() 方法
InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
/**
* 注意:配置文件一定要放到项目的根目录。
*/
@Test
public void run1() {
try {
FileInputStream is = new FileInputStream("src/my-ini.properties");
Properties pop = new Properties();
try {
pop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
Enumeration em = pop.propertyNames();
while(em.hasMoreElements()) {
String key = (String) em.nextElement();
String value = pop.getProperty(key);
System.out.println(key+"="+value);
} } catch (FileNotFoundException e) {
e.printStackTrace();
}
}
方法二:使用class变量的getResourceAsStream()方法
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
/**
* 注意:配置文件一定要放到当前目录下。
* (目录层次也可以从src下面的文件夹开始但不必包含src,且不必包含反斜杠开头。)
* 本方法在 PropertiesDemo1 类中
*/
@Test
public void run2() {
Properties pop = new Properties();
try{
InputStream in = PropertiesDemo1.class.getResourceAsStream("/my-ini.properties");
pop.load(in);
}catch(Exception ex) {
ex.printStackTrace();
}
Enumeration em = pop.propertyNames();
while(em.hasMoreElements()) {
String key = (String) em.nextElement();
String value = pop.getProperty(key);
System.out.println(key+"="+value);
} }
方法三:使用 class.getClassLoader() 所得到的 java.lang.ClassLoader 的 getResourceAsStream() 方法
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
/**
* 注意:
* 配置文件一定要放在 bin 目录下
* 注意 eclipse 软件自动将src中的配置文件复制到 bin 目录下
*/
@Test
public void run3() {
Properties pop = new Properties();
try{
InputStream in = PropertiesDemo1.class.getClassLoader().getResourceAsStream("my-ini.properties");
pop.load(in);
}catch(Exception ex) {
ex.printStackTrace();
}
Enumeration em = pop.propertyNames();
while(em.hasMoreElements()) {
String key = (String) em.nextElement();
String value = pop.getProperty(key);
System.out.println(key+"="+value);
}
}
方法四:使用 java.lang.ClassLoader 类的 getSystemResourceAsStream() 静态方法
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
/**
* 注意:
* 配置文件一定要放在 bin 目录下
*/
@Test
public void run4() {
Properties pop = new Properties();
try{
InputStream in = ClassLoader.getSystemResourceAsStream("my-ini.properties");
pop.load(in);
}catch(Exception ex) {
ex.printStackTrace();
}
Enumeration em = pop.propertyNames();
while(em.hasMoreElements()) {
String key = (String) em.nextElement();
String value = pop.getProperty(key);
System.out.println(key+"="+value);
}
}
方法五:使用 java.util.ResourceBundle 类的 getBundle() 方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
/**
* 注意:
* 配置文件一定要放在 bin 目录下
*/
@Test
public void run5() {
ResourceBundle rs = ResourceBundle.getBundle("my-ini"); String driver = rs.getString("driver");
String url = rs.getString("url");
String user = rs.getString("user");
String password = rs.getString("password"); System.out.println("driver="+driver);
System.out.println("url="+url);
System.out.println("user="+user);
System.out.println("password="+password);
}
方法六:使用 java.util.PropertyResourceBundle 类的构造函数
@Test
public void run6() {
File file = new File("src/my-ini.properties");
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
try {
PropertyResourceBundle prb = new PropertyResourceBundle(in); String driver = prb.getString("driver");
String url = prb.getString("url");
String user = prb.getString("user");
String password = prb.getString("password"); System.out.println("driver="+driver);
System.out.println("url="+url);
System.out.println("user="+user);
System.out.println("password="+password);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
引用博文:
JAVA操作properties配置文件——总结(Locale&ResourceBundle& PropertyResourceBundle(http://blog.csdn.net/fanxiaobin577328725/article/details/52071310)
properties配置文件读取操作总结【java笔记】的更多相关文章
- @Value和@PropertySource实现*.properties配置文件读取过程和实现原理
@Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1 配置使用步骤 (1)右击resource目录添加*.prooerties配置文件
- Java中Properties配置文件读取
以下实践的是Properties配置文件的基本操作方法.像spring使用xml做依赖注入时,这个配置文件起到非常实用的作用. 一.格式规范 参考wiki百科的格式简介:https://zh.wiki ...
- 使用Properties配置文件 InputStream与FileReader (java)
java 开发中,常常通过流读取的方式获取 配置文件数据,我们习惯使用properties文件,使用此文件需要注意 文件位置:任意,建议src下 文件名称:任意,扩展名为properties 文件内容 ...
- Spring Boot的properties配置文件读取
我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...
- properties配置文件读取
1.配置文件test.properties: test_123=admin 注:value 可用单引号,双引号,不用引号修饰 2.工具类PropertiesUtil: package com..... ...
- Java之properties文件读取
1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- .Net Core配置文件读取整理
一 .配置文件说明 1.配置,主要是 指在程序中使用的一些特殊参数,并且大多数 仅在程序启动的之后指定不需要修改. 2.在以前.Net项目中配置文件主要指app.config或web.config,但 ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
随机推荐
- java testng框架的windows自动化-自动运行testng程序上篇
本文旨在让读者简单了解testng的自动运行 怎么说呢,在网上已经有了各个前辈进行代码演示以及分享,我力争说到点子上 接上文,之前讲的大部分是juint的自动化代码运行,从未涉及到testng,但是在 ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Python练手例子(14)
79.字符串排序. #python3.7 if __name__ == '__main__': str1 = input('Input string:\n') str2 = input('Input ...
- python语法_模块_os_sys
os模块:提供对此操作系统进行操作的接口 os.getcwd() 获取python运行的工作目录. os.chdir(r'C:\USERs') 修改当前工作目录. os.curdir 返回当前目录 ( ...
- BIO,NIO与AIO的区别
Java NIO : 同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理.Java AIO(NIO.2 ...
- APP测试流程的总结
本规范基于app大小版本测试经验总结. 第一阶段:需求分析(技术+产品) 1. 新需求是否合理 2. 新旧需求时否存在冲突 3. 理出测试重点 4. 估算测试时间 5. 不熟悉的需求点,确认(负责人, ...
- [Swift]LeetCode233. 数字1的个数 | Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- php 168任意代码执行漏洞之php的Complex (curly) syntax
今天了解了php 168的任意代码执行漏洞,Poc: http://192.168.6.128/pentest/cms/php168/member/post.php?only=1&showHt ...