使用config.properties文件

来源:互联网 发布:银行表外业务数据 编辑:程序博客网 时间:2024/05/16 14:29
1. properties 文件就是文本文件,就像一楼说的,创建一个文本文件再改名就好了。
2. properties 文件格式就是key=value形的文件,#为注释行。比如:
#下面是个例子
userid = userID
password = user password

dburl = jdbc:db2://111.111.111:3333/sampledb


在java文件中读出config.properties


package Tes t ;import java . io . IOException ;import java . u t i l . Prope r t i e s ;public c l a s s Tes t {public s t a t i c void main ( S t r ing [ ] args ) {Prope r t i e s c o n f i g F i l e = new Prope r t i e s ( ) ;t ry {c o n f i g F i l e . load ( Tes t . c l a s s . getClassLoader ( ) .getResourceAsStream( " conf ig . prope r t i e s " ) ) ;S t r ing name = c o n f i g F i l e . getProper ty ( "name" ) ;System . out . pr int ln (name ) ;} cat ch ( IOException e ) {e . pr intSt a ckTr a c e ( ) ;}}}


0 0
原创粉丝点击