java 读取 properties 配置文件

package com.xing.test;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import java.io.*;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class ReadProperties {
public static void main(String[] args) throws IOException {

    String filePath = ReadProperties.class.getClassLoader().getResource("conf/demo.properties").getPath();

    /** 方法一
     *  基于ClassLoader读取配置文件
     *  有局限性 只能在类路径下比较方便
     */
    Properties properties = new Properties();
    // 注意这里的路径是根据target/classes 的路径写的
    InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("conf/demo.properties");
    properties.load(in);
    System.out.println("1111111111111---->:"+properties.getProperty("name"));


    /** 方法二
     *  基于InputStream读取配置文件
     *
     */
    Properties properties2 = new Properties();
    // 两种获取文件流的方式都可以,对于小文件第一种更快一点
    // 通过BufferedReader获取文件流
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
        properties2.load(bufferedReader);
        System.out.println("22222222222---->:"+properties2.getProperty("name"));
    }catch (Exception e){
        e.printStackTrace();
    }
    // 通过FileInputStreamm获取文件流
    InputStream in2 = new FileInputStream(new File(filePath));
    properties2.load(in2);
    System.out.println("22222222222---->:"+properties2.getProperty("name"));



    /** 方法三
     *  基于ResourceBundle读取配置文件
     *
     */
    // 1. 通过ResourceBundle.getBundle() 静态方法来获取文件 这种方式不需要添加后缀名
    ResourceBundle resourceBundle = ResourceBundle.getBundle("conf/demo");
    System.out.println("333333333333----->:"+resourceBundle.getString("name"));
    // 2. 通过InputStream读取文件
    InputStream in3 = new FileInputStream(new File(filePath));
    ResourceBundle resourceBundle2 = new PropertyResourceBundle(in3);
    System.out.println("333333333333----->:"+resourceBundle2.getString("name"));


    /** 方法四
     *  基于PropertiesConfiguration读取配置文件 需要使用第三方的包
     *
     */

    try {
        Configurations configurations = new Configurations();
        FileBasedConfigurationBuilder.setDefaultEncoding(PropertiesConfiguration.class, "UTF-8");
        PropertiesConfiguration propertiesConfiguration = configurations.properties(filePath);
        System.out.println("444444444444----->:"+propertiesConfiguration.getString("name"));


        //InputStream in4 = new FileInputStream(new File(filePath));
        PropertiesConfiguration propertiesConfiguration1 = new PropertiesConfiguration();
        propertiesConfiguration1.read(new BufferedReader(new FileReader(new File(filePath))));
        System.out.println("555555555555----->:"+propertiesConfiguration1.getString("name"));
    } catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
        e.printStackTrace();
    }
}

}

Properties properties = new Properties ();
try {
properties = PropertiesLoaderUtils.loadAllProperties (“setting.properties”);
System.out.println (new String (properties.getProperty (“warshipType.2”).getBytes (“iso-8859-1”), “gbk”));
} catch (IOException e) {
e.printStackTrace ();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值