Java——Properties 类

一.  Java Properties类

Properties 继承于 Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。

Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。

1.  构造方法

Properties 定义如下实例变量.这个变量持有一个Properties对象相关的默认属性列表。

Properties defaults;

Properties类定义了两个构造方法. 第一个构造方法没有默认值。

Properties()

第二个构造方法使用propDefault 作为默认值。两种情况下,属性列表都为空:

Properties(Properties propDefault)

2. 方法

String getProperty(String key) 用指定的键在此属性列表中搜索属性。
String getProperty(String key, String defaultProperty)用指定的键在属性列表中搜索属性。
void list (PrintStream streamOut) 将属性列表输出到指定的输出流。
void list(PrintWriter streamOut)将属性列表输出到指定的输出流。
void load(InputStream streamIn) throws IOException 从输入流中读取属性列表(键和元素对)。
Enumeration propertyNames( )按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
Object setProperty(String key, String value) 调用 Hashtable 的方法 put。
void store(OutputStream streamOut, String description)以适合使用  load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
clear ()清除所有装载的 键 - 值对

二. 读取 Properties 文件

1. load()

用  java.util.Properties 类的 load() 方法

InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2.  getBundle()

使用 java.util.ResourceBundle 类的 getBundle() 方法

示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.  PropertyResourceBundle类的构造函数

使用java.util.PropertyResourceBundle类的构造函数

示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4.  getResourceAsStream()①

使用 class 变量的 getResourceAsStream() 方法

示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5.  getResourceAsStream()②

使用 class.getClassLoader() 所得到的 java.lang.ClassLoader 的 getResourceAsStream() 方法

示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6.  getSystemResourceAsStream()

使用 java.lang.ClassLoader 类的 getSystemResourceAsStream() 静态方法

示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充:

Servlet 中可以使用 javax.servlet.ServletContext 的 getResourceAsStream()方法

示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

7. 总结:

但是最常用的还是通过 java.lang.Class 类的 getResourceAsStream(String name) 方法来实现,如下可以这样调用:

InputStream in = getClass().getResourceAsStream("资源Name");

或者下面这种也常用:

InputStream in = new BufferedInputStream(new FileInputStream(filepath));

 

三.  实例

1.  获取 JVM 的系统属性

Java虚拟机(JVM)有自己的系统配置文件(system.properties)

import java.util.Properties;

public class Test {
    public static void main (String[] args) {
        Properties properties = System.getProperties();
        properties.list(System.out);
    }
    
}

执行结果:

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\java\jdk\jre\bin
java.vm.version=25.151-b12
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=CN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=E:\IdeaProjects\demo
java.runtime.version=1.8.0_151-b12
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\java\jdk\jre\lib\endorsed
os.arch=amd64
java.io.tmpdir=C:\Users\时光·~1\AppData\Local\Temp\
line.separator=
......

2.  新建一个配置文件(Test.properties)

name = zth
age = 20
sex = M
package tiantian;

import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;

public class Test {
    public static void main (String[] args) throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\tiantian\\Test.properties"));

        Enumeration enum1 = properties.propertyNames();

        while (enum1.hasMoreElements()){
            String key = (String)enum1.nextElement();
            String value = properties.getProperty(key);
            System.out.println(key + " = "+value);
        }
    }
}

执行结果:

age = 20
name = zth
sex = M

3.  综合实例

根据 key 读取value

读取 properties 的全部信息

写入新的 properties 信息

package tiantian;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;

public class Test{
    // 根据 key 读取 value
    public static String getValue(String filepath,String key) throws Exception{
        Properties p = new Properties();
        InputStream in = new FileInputStream(filepath);
        p.load(in);
        String value = p.getProperty(key);
        //System.out.println(key + " = "+ value);
        return value;
    }

    // 获取 properties 的全部信息
    public static void getAll(String filepath) throws Exception{
        Properties p = new Properties();
        InputStream in = new FileInputStream(filepath);
        p.load(in);

        Enumeration en = p.propertyNames();
        while(en.hasMoreElements()){
            String key = (String)en.nextElement();
            String value = p.getProperty(key);
            System.out.println(key + " = "+ value);
        }
    }

    // 写入 Properties 信息
    public static void wirteProperties(String filePath,String key,String value) throws  Exception{
        Properties p = new Properties();
        InputStream in = new FileInputStream(filePath);
        p.load(in);
        p.setProperty(key,value);
        OutputStream out = new FileOutputStream(filePath);
        p.store(out,"");

    }

    public static void main(String[] args) throws Exception{
        String value = getValue("src\\tiantian\\Test.properties","name");
        System.out.println(value);
        System.out.println();
        getAll("src\\tiantian\\Test.properties");
        System.out.println();
        wirteProperties("src\\tiantian\\Test.properties","hob","mus");
        getAll("src\\tiantian\\Test.properties");
    }

}

执行结果:

zth

age = 20
name = zth
sex = M

age = 20
name = zth
hob = mus
sex = M

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值