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 ();
}

你可能感兴趣的:(后端)