java读取 properties配置文件的两种方式

java resource文件夹下面有一个application.properties的配置文件。文件里面是一行键值对信息 name=jack

方法一

        ResourceBundle rb = ResourceBundle.getBundle("application");
        String name= rb.getString("name");

     

方法二

   String fileName = 当前类名.class.getClassLoader().getResource("application.properties").getPath();     
    Properties properties =new Properties();
       properties.load(new FileInputStream(new File(fileName)));
       String name= properties.getProperty("name").trim();

tip: 第一中方式只需要配置文件的名字,不需要加properties

   第二种方法需要当前类的名字

原文地址:https://www.cnblogs.com/fangyuandoit/p/13713815.html