• Java-如何读取配置文件中参数信息


    Java中读取配置文件中参数:

    方法一:通过JDK中Properties来实现对配置文件的读取。

      Properties主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,能让用户够脱离程序本身去修改相关的变量设置。就像在Java中,其配置文件常为.properties文件,是以键值对的形式进行参数配置的。

    1、配置文件的设置

    sysName StefanieSun
    sysChinesName=孙燕姿
    sysBirthday:1976-07-02
    #空格、:、= 三种方式均可表示键值对的存在。

    2、新建读取类

    public class SystemProperties{
      //设置配置文件路径
      private final static String urlPath1 = "cn/com/yitong/util/system.properties";
      private final static String urlPath2 = "src/main/java/cn/com/yitong/util/system.properties";   
    private fianl static Properties properties = new Properties();

      方法1:使用classLoader来获取相对目录下文件(文件必须同SystemProperties同目录下;路径见"图1";此文件地址无需精确至"src/main/java/cn/com/yitong/util/system.properties",因为是同
          SystemProperties同目录)

      static{
        try{
          InputStream inputStream = ClassLoader.getSystemResourceAsStream(urlPath1);
          properties.load(inputStream);
          //
    properties.load(new InputStreamReader(ClassLoader.getSystemResourceAsStream(urlPath), "UTF-8"));方法类似
        }catch(IOExecption e){
          e.printStackTrace();
        }  
      }

      方法2:使用BufferedReader来读取配置文件。可以读取任意路径下的配置文件,并非一定同SystemProperties类同目录(此方法读取任意文件下配置文件,故相对路径为相对路径下的精确路径[需要相对精确的路
         径来确定文件])
      static{
        try{
          BufferedReader bufferedReader = new BufferedReader(new FileReader(urlPath2));
          properties.load(bufferedReader);
        }catch(IOException e ){
          e.printStackTrace();
        }
      }

      
      获取value值方法:
      
      public static String getValue(String key){
        return properties.getProperty(key).trim();
      }
      
      //通过key获取值,若值为null则返回defaultValue
      public static String getValue(String key,String defaultValue){
        return properties.getProperty(key,defaultValue);
      }

    }

    图1

    3、测试类:

    public class SystemPropertiesText {
    
        public static void main(String[] strings) {
          System.err.println(SystemProperties.getValue("sysName")); 
      }
    }

    方法二:通过ResourceBundle直接读取并取值

      方法一中读取配置文件的时候支持多种格式的配置文件(properties,md等),而ResourceBundle只能读取.properties格式文件。ResourceBundle主要用于获取文件国际化、本地化(详细内容请自行搜索了解,此处不过多说明)。

    1、配置文件信息同方法一

    2、此方法不需要特定的java类来编写,可直接获取。

    public class SystemPropertiesText{
      public static void main(String[] s){
        String urlPath = "
    cn/com/yitong/util/system.properties";
        ResourceBundle resourceBundle
    = ResourceBundle.getBundle(urlPath);     
        System.err.println(resourceBundle.getString(
    "sysName"));
      }
    }
    往外张望的人在做梦,向内审视的人才是清醒的
  • 相关阅读:
    fetch函数的使用-常见问题
    React的理解-入门指导
    Navicat for mysql 11.1.20激活
    sqlserver使用job删除过期备份文件
    sqlserver删除重复的数据
    提高code效率
    Wampserver红色橙色解决思路----端口冲突是关键
    java mvc框架系列总结ssh,ssm,servlet
    正则表达式入门案例C#
    SpringBoot开发之《兼容http和https同时访问时443端口被占用》
  • 原文地址:https://www.cnblogs.com/StefanieYang/p/12197951.html
Copyright © 2020-2023  润新知