JAVA操作properties配置⽂件
⼀、简介
<1> java中的properties⽂件是⼀种配置⽂件,主要⽤于表达配置信息,⽂件类型为*.properties,格式为⽂本⽂件,⽂件的内容是格式是"键=值"的格式。
<2> 在properties⽂件中,可以⽤"#"来作注释。
properties⽂件在Java编程中⽤到的地⽅很多,操作很⽅便。
⼆、Java的Properties类
属性映射(property map):是⼀种存储键/值对的数据结构。属性映射经常被⽤来存放配置信息。
它有三个特性:
1. 键和值⽃志字符串
2. 键/值对可以很容易地写⼊⽂件或从⽂件读出。
3. ⽤⼆级表存放默认值
实现属性映射的Java类被称为Properties(Java.util.Properties),此类是Java中⽐较重要的类,主要⽤于读取Java的配置⽂件,各种语⾔都有⾃⼰所⽀持的配置⽂件,配置⽂件中很多变量是经常改变的,这样做也是为了⽅便⽤户,让⽤户能够脱离程序本⾝去修改相关的变量设置。
此类是线程安全的:多个线程可以共享单个 Properties 对象⽽⽆需进⾏外部同步。
Properties类继承⾃Hashtable,如下:
构造⽅法:
Properties() 创建⼀个⽆默认值的空属性列表。
Properties(Properties defaults) 创建⼀个带有指定默认值的空属性列表。
它提供了⼏个主要的⽅法:
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 ():清除所有装载的 键 - 值对。该⽅法在基类中提供。
因为 Properties 继承于 Hashtable,所以可对 Properties 对象应⽤ put 和 putAll ⽅法。但不建议使⽤这两个⽅法,因为它们允许调⽤者插⼊其键或值不是 String 的项。相反,应该使⽤ setProperty ⽅法。
如果在“不安全”的 Properties 对象(即包含⾮ String 的键或值)上调⽤ store 或 save ⽅法,则该调⽤将失败。千金散尽还复来什么意思
类似地,如果在“不安全”的 Properties 对象(即包含⾮ String 的键)上调⽤ propertyNames 或 list ⽅法,则该调⽤将失败。
Properties类提供默认值的两种机制:
<1>在试图获得字符串值时制定默认值。(当键值不存在的时候,就会⾃动时⽤它)
String Property("title","Default title");
<2>如果觉得每次调⽤都指定默认值太⿇烦,那么就可以将所有的默认值放在⼀个⼆级属性映射中,并在主映射的构造器中提供映射。且⽤它来构造查询表。
1. Properties defaultSettings=new properties();
2. defaultSettings.setProperty("width","300");
3. defaultSettings.setProperty("height","200");
4. ...
5. Properties settings=new properties(defaultSettings);
6.
国庆最火的一句话注意:属性映射是没有层次结构的简单表。但是可以简单的使⽤java中的包命名⽅式来简单伪装⼀下层次结构。如果要存储复杂的配置信息,就应该使⽤Preferences类。
占星妙探三、Java读取Properties⽂件的⽅法
<1>Java虚拟机(JVM)有⾃⼰的系统配置⽂件(system.properties),我们可以通过下⾯的⽅式来获取。商家收款码怎么申请
1. //获取JVM的系统属性
2. import java.util.Properties;
3. public class ReadJVM {
4. public static void main(String[] args) {
5. Properties pps = Properties();
6. pps.list(System.out);
7. }
8. }
<2>使⽤J2SE API读取Properties⽂件的六种⽅法
1. 使⽤java.util.Properties类的load()⽅法⽰例:
InputStream in = new BufferedInputStream(new FileInputStream(name));Properties p = new Properties();p.load(in);详细⽰例⼀:
1. import java.io.FileInputStream;
2. import java.io.FileNotFoundException;
3. import java.io.IOException;
4. import java.util.Enumeration;
5. import java.util.Properties;
6. /*
7. * 新建⼀个配置⽂件(Test.properties),内容可以录⼊下⾯的语句。
8. * name=JJ
9. * Weight=4444
0. * Height=3333
1. *
2. * 注意:配置⽂件⼀定要放到项⽬的根⽬录。(此处没有异常处理)
3. *
4. */
5. public class getProperties {
6. public static void main(String[] args) throws FileNotFoundException, IOException {
7. Properties pps = new Properties();
8. pps.load(new FileInputStream("Test.properties"));
9. Enumeration enum1 = pps.propertyNames();//得到配置⽂件的名字
0. while(enum1.hasMoreElements()) {
1. String strKey = (String) Element();
2. String strValue = Property(strKey);
3. System.out.println(strKey + "=" + strValue);
4. }
5. }
6. }
详细⽰例⼆:
1. import java.io.BufferedInputStream;
2. import java.io.FileInputStream;
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5. import java.io.InputStream;
6. import java.io.OutputStream;
7. import java.util.Enumeration;
8. import java.util.Properties;
9.
0. //关于Properties类常⽤的操作
1. public class TestProperties {
2. //根据Key读取Value
3. public static String GetValueByKey(String filePath, String key) {
4. Properties pps = new Properties();
5. try {
6. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
7. pps.load(in);
8. String value = Property(key);
电脑如何定时关机9. System.out.println(key + " = " + value);
0. return value;
21.
2. }catch (IOException e) {
3. e.printStackTrace();
4. return null;
5. }
6. }
27.
8. //读取Properties的全部信息
9. public static void GetAllProperties(String filePath) throws IOException {
0. Properties pps = new Properties();
忽略不计1. InputStream in = new BufferedInputStream(new FileInputStream(filePath));
2. pps.load(in);
3. Enumeration en = pps.propertyNames(); //得到配置⽂件的名字
34.
5. while(en.hasMoreElements()) {
6. String strKey = (String) en.nextElement();
7. String strValue = Property(strKey);
8. System.out.println(strKey + "=" + strValue);
9. }
40.
1. }
42.
3. //写⼊Properties信息
4. public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
5. Properties pps = new Properties();
46.
7. InputStream in = new FileInputStream(filePath);
8. //从输⼊流中读取属性列表(键和元素对)
9. pps.load(in);
0. //调⽤ Hashtable 的⽅法 put。使⽤ getProperty ⽅法提供并⾏性。
1. //强制要求为属性的键和值使⽤字符串。返回值是 Hashtable 调⽤ put 的结果。
2. OutputStream out = new FileOutputStream(filePath);
3. pps.setProperty(pKey, pValue);
4. //以适合使⽤ load ⽅法加载到 Properties 表中的格式,
5. //将此 Properties 表中的属性列表(键和元素对)写⼊输出流
6. pps.store(out, "Update " + pKey + " name");
7. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论