java加载properties文件的几种方式

package com;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class LoadPropertiesFile {
private static final String DEFAULT_ENCODING = "UTF-8";
    
    /**
     * 使用java.util.Properties类的load()方法加载properties文件
     */
    private static void Method1() {
        try {
            // 获取文件流(方法1或2均可)
            InputStream inputStream = new BufferedInputStream(
            		new FileInputStream(
            				new File("src/main/resources/demo/jdbc.properties")
            				)); //方法1
//            InputStream inputStream = Thread.currentThread()
//            							.getContextClassLoader()
//            							.getResourceAsStream("jdbc.properties"); //方法2
            Properties prop = new Properties();
            
            prop.load(new InputStreamReader(inputStream, DEFAULT_ENCODING)); //加载格式化后的流
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method1: " + driverClassName);
            
        } catch (FileNotFoundException e) {
            System.out.println("properties文件路径有误!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用class变量的getResourceAsStream()方法
     * 注意:getResourceAsStream()方法的参数路径/包路径+properties文件名+.后缀
     */
    public static void Method2() {
        try {
            InputStream inputStream = LoadPropertiesFile.class.getResourceAsStream("/demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method2: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 
     * 注意:getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀
     */
    public static void Method3() {
        try {
            InputStream inputStream = LoadPropertiesFile.class.getClassLoader().getResourceAsStream("demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method3: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
     * getSystemResourceAsStream()方法的参数必须是包路径+文件名+.后缀
     */
    public static void Method4() {
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method4: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用java.util.ResourceBundle类的getBundle()方法
     * 注意:注意:这个getBundle()方法的参数相对同目录路径,并去掉.properties后缀,否则将抛异常
     */
    public static void Method5() {
        ResourceBundle resource = ResourceBundle.getBundle("demo");
        String driverClassName = resource.getString("driverClassName");
        
        System.out.println("Method5: " + driverClassName);
    }
    
    /**
     * 使用java.util.PropertyResourceBundle类的构造函数
     */
    public static void Method6(){
        ResourceBundle resource;
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties")));
            resource = new PropertyResourceBundle(inputStream);
            
            String driverClassName = resource.getString("driverClassName");
            System.out.println("Method6: " + driverClassName);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        Method1();
        Method2();
        Method3();
        Method4();
        Method5();
        Method6();
    }
}

5,6是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容;

其中最重要的就是每种方式加载文件时,文件的路径一定要对。

 

properties文件以及项目结:

1

2

3

4

5

#\u73af\u4fdd\u5927\u68c0\u67e5\u6570\u636e\u5e93\u6570\u636e\u5e93\u76f8\u5173

driverClassName=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/mydatabase?useUnicode\=true&characterEncoding\=utf-8&generateSimpleParameterMetadata\=true

username=root

password=test123

 

1:带类加载器:

this.getClass().getClassLoader().getResourceAsStream("testVariables.bpmn") 从classpath根目录下加载指定名称的文件 

this.getClass().getResourceAsStream("testVariables.bpmn") 从当前包下加载指定名称的文件

 this.getClass().getResourceAsStream("/testVariables.bpmn") 从classpath根目录下加载指定名称的文件

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值