欢迎进入西安甲骨文培训机构 乘车路线| 关于我们| 设为首页| 加入收藏
logo 服务热线
1 2 3 3

新闻动态

联系我们

咨询报名电话:
029-85568080,029-85427081
传真:029-85568080
地址:陕西省西安市长安南路355号华银大厦(西北政法大学老校区对面西安银行南侧)

优秀学员

技术中心 当前位置:首页 > 新闻动态 > 技术中心

java properties文件的使用

点击: 时间:2013-12-30
      经常看到java程序中带properties文件(包括log4j,junit的使用),作为一个java所知甚少的程序员,有必要搞清楚这个问题.

      首先,这个文件经常是作为资源文件的,maven,sbt中资源文件一般还独立一个src/java/resource文件夹,资源文件一般是通过getResouce,getResourceAsStream(获取对应InputStream)获得.如:

      Test.class.getResource("file3.txt")

      Test.class.getResource("/file3.txt")

      Test.class.getClassLoader.getResource("/file3.txt")

参数可以是相对路径和绝对路径,相对路径是在对应class文件(classpath)下找的,绝对路径则是顶层classloader那一层找的.

      properties文件使用需要import java.util.Properties,如在scala shell中输入:

      import java.util.Properties
      val x = getClass.getResourceAsStream("/a.properties")
      val prop = new Properties
      prop.load(x)
      prop.getProperty("a") ==> 返回null
      prop.getProperty("IcisReport.contextPath") ==> 返回/IcisReport
对应的a.properties如:

# IcisReport的ip
IcisReport.server.ip=192.168.3.143
# IcisReport的端口
IcisReport.server.port=8080
# IcisReport的上下文路径
IcisReport.contextPath=/IcisReport