JAVA读取Properties文件的六种方式

本文详细介绍了五种在Java中加载属性文件的方法,包括使用FileInputStream、ResourceBundle、Class.getResourceAsStream、ClassLoader.getSystemResourceAsStream及servlet上下文,每种方法都有其适用场景和注意事项。

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

package com.kerwin.springboot;

import org.junit.Test;

import java.io.*;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * @ClassName: PropertiesLoadTest
 * @Description:
 * @version: v1.0.0
 * @Author: d
 * @Date: 2018-12-18 10:43
 */
public class PropertiesLoadTest
{

    @Test
    public void loadProperties1() throws IOException
    {
        /** 注:FileInputStream 加载文件时,需使用文件的全路径 */
        InputStream in = new BufferedInputStream(new FileInputStream("E:springboot/src/test/resources/prop/test.properties"));

        Properties properties = new Properties();

        properties.load(in);

        System.out.println(properties);
    }

    /** 注:ResourceBundle.getBundle 加载配置时,不能写后缀,且路径为项目相对路径即可 */
    @Test
    public void loadProperties2(){
        ResourceBundle rb = ResourceBundle.getBundle("prop/test", Locale.getDefault());

        String my_name = rb.getString("my_name");

        String my_password = rb.getString("my_password");
        System.out.println("name: "+my_name+"; password: "+my_password);
    }

    /** 注:PropertyResourceBundle 与 Properties 用法差不多,主要是在获取参数的区别 */
    @Test
    public void loadProperties3() throws IOException
    {
        InputStream in = new BufferedInputStream(new FileInputStream("E:springboot/src/test/resources/prop/test.properties"));
        PropertyResourceBundle rb = new PropertyResourceBundle(in);

        String my_name = rb.getString("my_name");
        String my_password = rb.getString("my_password");

        System.out.println("name: "+my_name+"; password: "+my_password);
    }

    /** 注:getResourceAsStream 须是本类的类文件去调用,路径为项目的相对或绝对路径 */
    @Test
    public void loadProperties4() throws IOException
    {
        InputStream in = PropertiesLoadTest.class.getResourceAsStream("/prop/test.properties");

        if (in != null){
            Properties pr = new Properties();

            pr.load(in);

            System.out.println(pr);
        }
    }

    /** 注:使用ClassLoader.getSystemResourceAsStream() 路径为项目内部的相对路径 */
    @Test
    public void loadProperties5() throws IOException
    {
        InputStream in = ClassLoader.getSystemResourceAsStream("prop/test.properties");
        Properties pr = new Properties();

        pr.load(in);

        System.out.println(pr);
    }

    /** 注:此方式只使用于servlet中,context为servlet容器 */
    public void loadProperties6(){
        InputStream in = context.getResourceAsStream("prop/test.properties");
        Properties p = new Properties();
        p.load(in);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值