首页 > 编程知识 正文

java获取配置文件参数,properties格式

时间:2023-05-04 20:46:56 阅读:173954 作者:1330

publicclassgetproperties { privatestaticpropertiesconfig=null; static { inputstream in=get properties.class.getclass loader (.getresourceasstream (' config.properties ' ) ); config=newP .

公共类获取属性{

privatestaticpropertiesconfig=null;

静态{

' config.properties ';

config=new Properties (;

try {

config.load(in;

in.close (;

}catch(ioexceptione ) {

}

}

基于//key读取值

publicstaticstringreadvalue (string key ) {

//properties props=new properties (;

try {

string value=config.getproperty (key;

返回值;

}catch(exceptione ) {

e .打印任务跟踪(;

返回空值;

}

}

读取属性的所有信息

publicstaticvoidreadallproperties {

try {

enumeration en=config.property names (;

while(en.hasmoreelements () ) ) ) )。

stringkey=(string ) en.nextElement );

字符串属性=config.getproperty (key );

}

}catch(exceptione ) {

e .打印任务跟踪(;

}

}

publicstaticvoidmain (string args [ ] ) {

//stringlasaphone=config.getproperty (las aphone );

//system.out.println(lasaphone );

//system.out.println (get phone.read value (' las aphone ' ) );

get properties.read all properties (;

}

}

config.properties文件:

#area message phone :

Java.lang.exceptioninitializererror

caused by : Java.lang.dq dch/pat Java.io.reader.(reader.Java :61 ) )。

at Java.io.input streamreader.(input streamreader.Java :80 )

at Java.util.properties.load (properties.Java :189 )

atcom.liuchen.get properties.(get properties.Java :16 )

Exception in thread 'main '

虽然在src目录中有记载,但还在报告那个错误

这也不行,这是怎么回事?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。