Properties类一些常用的用法

本文详细介绍了Java中操作属性文件的实现方式,包括加载、存储、获取和设置属性信息的方法,以及如何通过Properties类实现属性文件的读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码:

package test.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class TestProperties {

    public static void main(String[] args) throws FileNotFoundException,
            IOException, Exception {
        Properties pro = init.getPro();
        String apple = pro.getProperty("apple");
        System.out.println(apple);

        // list()方法:该方法多用于调试,开发中很少用
        pro.list(System.out);

        // test2: store()方法生成持久化文件
        // init.proStore();

    }

}

// 操作属性文件
class init {

    public static Properties proList() {
        Properties pro = new Properties();
        pro.setProperty("11", "one");
        pro.setProperty("22", "two");
        pro.setProperty("33", "throw");

        pro.list(System.out);// 该方法多用于调式,开发中很少用

        return pro;
    }

    public static void proLoad() throws Exception {
        File f = new File("fruit.properties");
        FileInputStream is = new FileInputStream(f);

        Properties pro = new Properties();
        pro.load(is);

        pro.list(System.out);
    }

    public static void proStore() throws Exception {

        Properties pro = proList();

        FileOutputStream fos = new FileOutputStream("fruit1.properties");

        pro.store(fos, "TEST");

        fos.close();
    }

    public static Properties getPro() throws FileNotFoundException, IOException {

        Properties pro = new Properties();

        // 在根目录下
        File f = new File("fruit.properties");
        if (f.exists()) {
            pro.load(new FileInputStream(f));
        } else {
            pro.setProperty("apple", "Reflect.Apple");
            pro.setProperty("one", "Reflect.One");

            // 想要将这个集合中的字符串键值信息持久化存储到文件中,需要关联输出流
            // store()方法是持久化,没有这个方法,则不会生成fruit.properties文件
            pro.store(new FileOutputStream(f), "FRUIT CLASS");
        }

        return pro;
    }
}

 

api:

load()和store()

void load(InputStream inStream)

Reads a property list (key and element pairs) from the input byte stream.
void store(OutputStream out, String comments)
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the

这是一对。

以下是另一对:

load(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
store(Writer writer, String comments)
Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.

 

 

附1:

1.Properties类与Properties配置文件

  Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2.Properties中的主要方法

(1)load(InputStream inStream)

   这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象如下面的代码:

Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)

   这个方法将Properties类对象的属性列表保存到输出流中如下面的代码:

FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

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

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

(3)getProperty/setProperty

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

 

附2:一些网上的资料:Java 加载Properties文件的六种方式:http://blog.youkuaiyun.com/yz394777014/article/details/4330583

1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用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);

 

转载于:https://www.cnblogs.com/gmq-sh/p/4832388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值