Java读取properties文件内容

来源:互联网 发布:nodejs 加载js 编辑:程序博客网 时间:2024/06/05 03:38
properties文件的内容本身是按照键值对的形式存储的,所以一般情况下是按照取“键”得“值”
properties:
config-jya.properties
### url ###
pathURL= http://192.168.0.178:8080/JyaMs/

方法1:
创建一个UtilProperties Java文件,里面单独写读取的代码
public class UtilProperties {
       private static String fileName = "/config-jya.properties";
       public static String getProperty(String name) {
            Properties prop = new Properties();
            InputStream in = UtilProperties.class .getResourceAsStream(fileName);
             try {
                  prop.load(in);
                   return prop.getProperty(name);
            } catch (IOException e) {
                  e.printStackTrace();
            } finally {
                   try {
                        in.close();
                  } catch (IOException e) {
                        e.printStackTrace();
                  }
            }
             return null ;
      }
}
然后在其他类方法中可以直接调用,OK就这么简单了!
String pathURL = UtilProperties.getProperty("pathURL" );

方法2:
此方法用于那种启动项目就立马加载的properties配置文件,需要配置监听器
创建一个类
public class ContextInitListener implements ServletContextListener
使得该类成为一个监听器。用于监听整个容器生命周期的,主要是初始化和销毁的。

类创建后要在web.xml配置文件中增加一个简单的监听器配置,即刚才我们定义的类。
<listener>
    <description> ServletContextListener</description >
    <listener-class> com.jya.util.ContextInitListener</listener-class >
</listener >

配置好监听器后我们开始编写ContextInitListener 的代码。实现接口后会自动生成两个方法,初始化和销毁,我们就只贴出这个吧,另一个没什么用。
web项目通常来说,一般来说相对路径是在WEB-INF/classes,获取该路径下的文件,最好用getClass().getResourceAsStream("/baseconfig.properties");比较简单。
public class ContextInitListener implements ServletContextListener {

       private static String fileName = "/config-jya.properties";

       @Override
       public void contextDestroyed(ServletContextEvent arg0) {

      }
       @Override
       public void contextInitialized(ServletContextEvent arg0) {
            Properties props = new Properties();
            InputStream inputStream = getClass().getResourceAsStream(fileName );
             try {
                  props.load(inputStream);
                  String tempPath = (String) props.get( "pathURL");
                  String tempPath1 = props.getProperty( "pathURL");//两种写法获取的结果竟然是一样的
                  System. out.println(tempPath+"@@@@@@@@@@@@@@@@@" );
                  System. out.println(tempPath1+"#################" );
            } catch (IOException ex) {
                  ex.printStackTrace();
            }
      }
}
方法3:
启动项目就立马加载的properties配置文件,用的是servlet初始化init
<servlet>
   <servlet-name> init</servlet-name>
   <servlet-class>com.jya.util.InitServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

servlet文件
public class InitServlet extends HttpServlet{
       private static final long serialVersionUID = 1049638592092350823L;
       @Override
       public void init() throws ServletException {
             super.init();
            
            String fileName = "/config-jya.properties";
            Properties props = new Properties();
            InputStream inputStream = getClass().getResourceAsStream(fileName);
             try {
                  props.load(inputStream);
                   //String tempPath = (String) props.get("path");
                  String pathURL = props.getProperty( "pathURL");//两种写法获取的结果竟然是一样的
                  System. out.println(pathURL+"2222222222222222222222" );
            } catch (IOException ex) {
                  ex.printStackTrace();
            }
      }
}
0 0
原创粉丝点击