java资源文件支持类-Properties类的常用用法

java中的Properties类是HashTable的一个实现,属于map类型,代表一系列的属性键值对。

public class Properties
extends Hashtable;// 继承自Hashtable,属于Map类型;
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.
Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a “compromised” Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a “compromised” Properties object that contains a non-String key.
——-设置属性值并输出
package com.mldn;
import java.util.Properties;
public class PropertiesDemo
{
        public static void main(String[] args)
        {
                Properties pro = new Properties();        // 实例化一个属性对象
                pro.setProperty(“JN”, “Jinan”);        // 设置属性,调用put方法
                pro.setProperty(“BJ”, “Beijing”);
                pro.setProperty(“SH”, “Shanghai”);
                pro.setProperty(“Qd”, “Qingdao”);
                System.out.println(“属性存在:” + pro.getProperty(“JN”));                // 获取属性值
                System.out.println(“属性不存在:” + pro.getProperty(“tt”));        // 属性不存在,返回null
                System.out.println(“属性不存在,显示默认值:” + pro.getProperty(“SS”, “Shaitai”));        // 可以设定默认值,不存在时取默认值
        }
}
/*
ubuntu@xu-desktop:~$ java com.mldn.PropertiesDemo
属性存在:Jinan
属性不存在:null
属性不存在,显示默认值:Shaitai
*/
————-存储属性设置到文件:
public void store(OutputStream out,
                  String comments)
           throws IOException;// 以字节流形式保存到磁盘@!

package com.mldn;
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo
{
        public static void main(String[] args)
        {
                Properties pro = new Properties();        // 实例化一个属性对象
                pro.setProperty(“JN”, “Jinan”);        // 设置属性,调用put方法
                pro.setProperty(“BJ”, “Beijing”);
                pro.setProperty(“SH”, “Shanghai”);
                pro.setProperty(“Qd”, “Qingdao”);
                File file = new File(File.separator + “home” + File.separator + “ubuntu” + File.separator + “a.properties”);               
                try
                {
                        pro.store(new FileOutputStream(file), “Area info”);        // 存储, 并设定注释
                }
                catch (FileNotFoundException e)
                {
                        e.printStackTrace();
                }
                catch (IOException e)
                {
                        e.printStackTrace();               
                }
        }
}
/* 这是该属性文件的内容:
#Area info
#Tue Apr 06 20:36:03 CST 2010
BJ=Beijing
JN=Jinan
SH=Shanghai
Qd=Qingdao*/          
——–读取已存在的属性文件到属性对象中:
public void load(InputStream inStream)
          throws IOException;// 载入属性到对象中
package com.mldn;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo
{
        public static void main(String[] args)
        {
                Properties pro = new Properties();        // 实例化一个属性对象
                File file = new File(File.separator + “home” + File.separator + “ubuntu” + File.separator + “a.properties”);               
                try
                {
                        pro.load(new FileInputStream(file));        // 读取已存在的属性文件
                }
                catch (FileNotFoundException e)
                {
                        e.printStackTrace();
                }
                catch (IOException e)
                {
                        e.printStackTrace();               
                }
                System.out.println(“属性BJ存在:” + pro.getProperty(“BJ”));
        }
}
/*
ubuntu@xu-desktop:~$ java com.mldn.PropertiesDemo
属性BJ存在:Beijing
*/
———–以XML格式保存属性文件:
public void storeToXML(OutputStream os,
                       String comment)
                throws IOException;
package com.mldn;
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo
{
        public static void main(String[] args)
        {
                Properties pro = new Properties();        // 实例化一个属性对象
                pro.setProperty(“JN”, “Jinan”);        // 设置属性,调用put方法
                pro.setProperty(“BJ”, “Beijing”);
                pro.setProperty(“SH”, “Shanghai”);
                pro.setProperty(“Qd”, “Qingdao”);
                File file = new File(File.separator + “home” + File.separator + “ubuntu” + File.separator + “a.xml”);               
                try
                {
                        pro.storeToXML(new FileOutputStream(file), “Area info”);        // 存储, 并设定注释
                }
                catch (FileNotFoundException e)
                {
                        e.printStackTrace();
                }
                catch (IOException e)
                {
                        e.printStackTrace();               
                }
        }
}
/* 以XML格式保存:Area info
Beijing
Jinan
Shanghai
Qingdao
*/
———-从XML中读取属性:
public void loadFromXML(InputStream in)
                 throws IOException,
                        InvalidPropertiesFormatException;// 读取xml格式的文本文档;

package com.mldn;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class LoadXMLDemo
{
        public static void main(String[] args)
        {
                Properties pro = new Properties();        // 实例化一个属性对象
                File file = new File(File.separator + “home” + File.separator + “ubuntu” + File.separator + “a.xml”);
                try
                {
                        pro.loadFromXML(new FileInputStream(file));        // 从xml文件流中读入属性信息到当前属性对象
                }
                catch (FileNotFoundException e)
                {
                        e.printStackTrace();
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                System.out.println(pro);        // 输出当前属性文件
        }
}
/*
ubuntu@xu-desktop:~$ java com.mldn.LoadXMLDemo
{BJ=Beijing, JN=Jinan, SH=Shanghai, Qd=Qingdao}
// 这是从xml中读取的
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java资源文件支持类-Properties类的常用用法
本文地址:http://www.xiupu.net/archives-127.html
关注公众号:嗅谱网

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!