栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java读取.properties配置文件的几种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java读取.properties配置文件的几种方式

Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配置文件就比较容易。

介绍几种读取方式:

1、基于ClassLoder读取配置文件

注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。

Properties properties = new Properties();
  // 使用ClassLoader加载properties配置文件生成对应的输入流
  InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");
  // 使用properties对象加载输入流
  properties.load(in);
  //获取key对应的value值
  properties.getProperty(String key);

2、基于 InputStream 读取配置文件

注意:该方式的优点在于可以读取任意路径下的配置文件

Properties properties = new Properties();
  // 使用InPutStream流读取properties文件
  BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/config.properties"));
  properties.load(bufferedReader);
  // 获取key对应的value值
  properties.getProperty(String key);

3、通过 java.util.ResourceBundle 类来读取,这种方式比使用 Properties 要方便一些

1>通过 ResourceBundle.getBundle() 静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可

    properties.getProperty(String key);
   //config为属性文件名,放在包com.test.config下,如果是放在src下,直接用config即可 
   ResourceBundle resource = ResourceBundle.getBundle("com/test/config/config");
   String key = resource.getString("keyWord"); 

2>从 InputStream 中读取,获取 InputStream 的方法和上面一样,不再赘述

ResourceBundle resource = new PropertyResourceBundle(inStream);

注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取)或com/test/config/config(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用config.properties或config即可。

下面附上几种方式的测试代码,仅供参考:

package com.test.properties;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.springframework.core.io.support.PropertiesLoaderUtils;


public class TestProperties {


  
  private static void printAllProperty(Properties props)
  {
    @SuppressWarnings("rawtypes")
    Enumeration en = props.propertyNames();
    while (en.hasMoreElements())
    {
      String key = (String) en.nextElement();
      String value = props.getProperty(key);
      System.out.println(key + " : " + value);
    }
  }

  
  public static String getProperties_1(String filePath, String keyWord){
    Properties prop = null;
    String value = null;
    try {
      // 通过Spring中的PropertiesLoaderUtils工具类进行获取
      prop = PropertiesLoaderUtils.loadAllProperties(filePath);
      // 根据关键字查询相应的值
      value = prop.getProperty(keyWord);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return value;
  }

  
  public static void getProperties_1(String filePath){
    Properties prop = null;
    try {
      // 通过Spring中的PropertiesLoaderUtils工具类进行获取
      prop = PropertiesLoaderUtils.loadAllProperties(filePath);
      printAllProperty(prop);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  
  public static String getProperties_2(String filePath, String keyWord){
    Properties prop = new Properties();
    String value = null;
    try {
      // 通过输入缓冲流进行读取配置文件
      InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
      // 加载输入流
      prop.load(InputStream);
      // 根据关键字获取value值
      value = prop.getProperty(keyWord);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return value;
  }

  
  public static void getProperties_2(String filePath){
    Properties prop = new Properties();
    try {
      // 通过输入缓冲流进行读取配置文件
      InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
      // 加载输入流
      prop.load(InputStream);
      printAllProperty(prop);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  
  public static String getProperties_3(String filePath, String keyWord){
    Properties prop = new Properties();
    String value = null;
    try {
      InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
      prop.load(inputStream);
      value = prop.getProperty(keyWord);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return value;
  }

  
  public static void getProperties_3(String filePath){
    Properties prop = new Properties();
    try {
      InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
      prop.load(inputStream);
      printAllProperty(prop);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }


  public static void main(String[] args) {
    // 注意路径问题
    String properties_1 = getProperties_1("com/test/config/config.properties", "wechat_appid");
    System.out.println("wechat_appid = " + properties_1);
    getProperties_1("com/test/config/config.properties");
    System.out.println("*********************************************");
    // 注意路径问题
    String properties_2 = getProperties_2("configure/configure.properties", "jdbc.url");
    System.out.println("jdbc.url = " + properties_2);
    getProperties_2("configure/configure.properties");
    System.out.println("*********************************************");
    // 注意路径问题
    String properties_3 = getProperties_3("/com/test/config/config.properties", "wechat_appid");
    System.out.println("wechat_appid = " + properties_3);
    getProperties_3("/com/test/config/config.properties");
  }
}

到此这篇关于Java读取.properties配置文件的几种方式的文章就介绍到这了,更多相关Java 读取properties 配置内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号