java properties 类_java基础--properties 类使用

一、Java Properties类

Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

Properties类继承自Hashtable,如下:

9ae3ccb8ccc9faed4cc39457caaac4a0.png

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

二、Hashtable 与HashMap的区别

1、主要:Hashtable线程安全,同步,效率相对低下

HashMap 线程不安全,非同步,效率相对高

2、父类:Hashtable 是 Dictionary HashMap 是 AbstractMap

3、null: Hashtable键与值不能为null

HashMap 键最多一个null,值可以多个null

三、Properties

1、作用:读写资源配置文件

2、键与值只能为字符串

3、方法:

setProperty(String key, String value)

getProperty(String key)

getProperty(String key, String defaultValue)

后缀:.properties

store(OutputStream out, String comments)

store(Writer writer, String comments)

load(InputStream inStream)

load(Reader reader)

.xml

storeToXML(OutputStream os, String comment) :UTF-8字符集

storeToXML(OutputStream os, String comment, String encoding)

loadFromXML(InputStream in)

三、相对路径与绝对路径

1、绝对路径 : 盘符: /

2、相对路径 : 当前项目、工程

四、类路径加载资源文件

类所在的根路径

1、类.class.getResourceAsStream("/")

2、Thread.currentThread().getContextClassLoader().getResourceAsStream("")

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bjsxt.others.pro;importjava.util.Properties;/*** Properties 资源配置文件的读写

* 1、key 与value 只能为字符串

* 2、存储与读取

* setProperty(String key, String value)

* getProperty(String key, String defaultValue)

*@authorAdministrator

**/

public classDemo01 {/***@paramargs*/

public static voidmain(String[] args) {//创建对象

Properties pro =newProperties();//存储

pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");//pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");

pro.setProperty("user", "scott");

pro.setProperty("pwd", "tiger");//获取

String url =pro.getProperty("url","test");

System.out.println(url);

}

}

demo1

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bjsxt.others.pro;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Properties;/*** 使用Properties 输出到文件

* 资源配置文件:

*

* 1、.properties

* store(OutputStream out, String comments)

store(Writer writer, String comments)

2、.xml

storeToXML(OutputStream os, String comment) :UTF-8字符集

storeToXML(OutputStream os, String comment, String encoding)

*@authorAdministrator

**/

public classDemo02 {/***@paramargs

*@throwsIOException

*@throwsFileNotFoundException*/

public static void main(String[] args) throwsFileNotFoundException, IOException {//创建对象

Properties pro =newProperties();//存储

pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");

pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");

pro.setProperty("user", "scott");

pro.setProperty("pwd", "tiger");//存储到e:/others 绝对路径 盘符://pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");//pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");//使用相对路径 当前的工程//pro.store(new FileOutputStream(new File("db.properties")), "db配置");//pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");

pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");

}

}

demo2

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bjsxt.others.pro;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.Properties;/*** 使用Properties读取配置文件

* 资源配置文件:

* 使用相对与绝对路径读取

* load(InputStream inStream)

load(Reader reader)

loadFromXML(InputStream in)

*@authorAdministrator

**/

public classDemo03 {/***@paramargs

*@throwsIOException

*@throwsFileNotFoundException*/

public static void main(String[] args) throwsFileNotFoundException, IOException {

Properties pro=newProperties();//读取 绝对路径//pro.load(new FileReader("e:/others/db.properties"));//读取 相对路径

pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));

System.out.println(pro.getProperty("user", "bjsxt"));

}

}

demo3

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bjsxt.others.pro;importjava.io.IOException;importjava.util.Properties;/*** 使用类相对路径读取配置文件

* bin

*@authorAdministrator

**/

public classDemo04 {/***@paramargs

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

Properties pro=newProperties();//类相对路径的 / bin//pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));//"" bin

pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));

System.out.println(pro.getProperty("user", "bjsxt"));

}

}

demo4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值