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

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

posted @ 2019-06-24 21:22  妖君你好  阅读(149)  评论(0编辑  收藏  举报