首页 > 编程知识 正文

安卓properties配置文件,java properties类

时间:2023-05-03 20:41:32 阅读:145597 作者:2958

1 .属性类和属性配置文件

Properties类继承自Hashtable类,用于实现Map接口。 此外,还可以使用键-值对格式保存属性集。 但是,Properties有一个特殊的地方。 键和值都是字符串类型。

2 .属性中的主要方法

(1)加载(输入流) )。

此方法可以从与. properties属性文件相对应的文件输入流中将属性列表加载到properties类对象中。 如下面的代码所示

属性pro=new properties (;

fileinputstreamin=new file inputstream (a.properties );

pro.load(in;

in.close (;

(2)存储输出流输出,字符串注释) ) ) )。

此方法将Properties类对象的属性列表保存到输出流中。 如下面的代码所示

fileoutputstreamofile=new file output stream (file,' a.properties ' );

pro.store(ofile,' Comment ' );

oFile.close (;

如果comments不为空,则保存的属性文件的第一行为表示注释信息的#comments。 如果为空,则没有评论信息。

注释信息后面跟着属性文件的当前保存时间信息。

)3)获取属性/设置属性

两种方法是分别获取和设置属性信息。

3 .代码示例

属性文件a.properties如下:

名称=根

帕斯=刘

key=value

a .装入属性列表并生成属性文件b.properties。 代码如下。

1 import Java.io.buffered inputstream;

2 import java.io.FileInputStream;

3 import Java.io.file output stream;

4 import java.io.InputStream;

5 import java.util.Iterator;

6 import java.util.Properties;

7

8公共类属性测试{

9 publicstaticvoidmain (字符串[ ] args ) {

10 Properties prop=new Properties (;

11特里{

读取12 //属性文件a.properties

13 inputstream in=newbufferedinputstream (new file inputstream (' a.properties ' );

14prop.load(in; //加载属性列表

15 iterator it=prop.stringpropertynames ().iterator );

16while(it.Hasnext () ) ) ) ) )。

17 String key=it.next (;

18 system.out.println (key ' : ' prop.getproperty ) key );

19 }

20 in.close (;

21

将22 ///属性保存到b.properties文件

23 fileoutputstreamofile=new file output stream (' b.properties ',true ); //true表示额外打开

24prop.setproperty('Phone ',' 10086 );

25prop.store(ofile,' The New properties file ' );

26 oFile.close (;

27 }

28catch(exceptione ) {

29system.out.println(e;

30 }

31 }

32 }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。