使用Properties读写属性文件
2017-08-11 11:58
176 查看
Properties类是HashTable类的子类,正如它的名字所暗示的,该对象在处理属性文件时特别方便。Properties类可以把Map对象和属性文件关联起来,从而可以把Map对象中的key-value对写入属性文件中,也可以把属性文件中的"属性名=属性值"加载到Map对象中。由于属性文件里的属性名、属性值只能是字符串类型,所以Properties里的Key、value都是字符串类型。该类提供了如下三个方法来修改Properties里的Key、value。
--String getProperty(String key):获取Properties中指定属性名对象的属性值,类似于Map的get(Object key)。
--String getProperty(String key,String defaultValue):该方法与前一个方法基本相似。该方法多一个功能,如果Properties中不存在指定的key时,则该方法指定默认值。
--Object setproperty(String key,String value):设置属性值,类似于HashTable的put()方法。除此之外,它还提供了两个读写属性文件的方法。
--void load(InputStream inStream):从属性文件中加载key-value对,把加载到的key-value对追加到Properties里。
--void store(OutputStream out,String comments):将Properties中的key-value对输出到指定的属性文件。
--String getProperty(String key):获取Properties中指定属性名对象的属性值,类似于Map的get(Object key)。
--String getProperty(String key,String defaultValue):该方法与前一个方法基本相似。该方法多一个功能,如果Properties中不存在指定的key时,则该方法指定默认值。
--Object setproperty(String key,String value):设置属性值,类似于HashTable的put()方法。除此之外,它还提供了两个读写属性文件的方法。
--void load(InputStream inStream):从属性文件中加载key-value对,把加载到的key-value对追加到Properties里。
--void store(OutputStream out,String comments):将Properties中的key-value对输出到指定的属性文件。
public class PropertiesTest { public static void main(String[] args)throws Exception { Properties props = new Properties(); props.setProperty("username", "yeeku"); props.setProperty("password", "123456"); props.store(new FileOutputStream("a.ini"),"comment line"); Properties props2 = new Properties(); props2.setProperty("gender", "male"); props2.load(new FileInputStream("a.ini")); System.out.println(props2); } }输出结果如下:
{password=123456,gender=male,username=yeeku}上面程序还在当前路径下生成了一个a.ini文件,该文件的内容如下:
#comment line #Fri Aug 11 11:42:12 GMT+08:00 2017 password=123456 username=yeekuProperties 可以把key-value对以XML文件的形式保存起来,也可以从XML文件中加载key-value对,用法与此类似。
相关文章推荐
- 使用JAVA读写Properties属性文件
- 使用Properties读写属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用Properties读写属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用java读写properties文件属性
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件的方法详解
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- [转载收藏]使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用JAVA读写Properties属性文件
- 使用java读写properties文件属性
- 使用JAVA读写Properties属性文件