Properties properties=new Properties();
InputStream is=null;
try{
is=getClass().getResourceAsStream(FILE);
properties.load(is);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(is!=null) is.close();
}catch(IOException e){
e.printStackTrace();
}
}
String port=properties.getProperty("PORT");
InputStream is=null;
try{
is=getClass().getResourceAsStream(FILE);
properties.load(is);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(is!=null) is.close();
}catch(IOException e){
e.printStackTrace();
}
}
String port=properties.getProperty("PORT");
|
+--java.util.Dictionary
|
+--java.util.Hashtable
|
+--java.util.Properties
if(in==null){
System.out.println("in is null");
return;
}
try{
properties.load(in);
System.out.println(properties.getProperty("Port"));
}
catch(IOException e){
System.out.println(e.getMessage());
}那个文件存在,但是怎么in总是null呀,这是为什么呀?
Properties prop = new Properties();
FileInputStream fis = null;
int port = 0;
boolean inflag = false;
try {
fis = new FileInputStream(new File("." + File.separator + fileName));
prop.load(fis);
port = Integer.parseInt(prop.getProperty("PORT"));
fis.close();
inflag = true;
}
catch (IOException ex1) {
port = 8888;
inflag = false ;
}
return inflag;
}// end of readConfigFile
#[SectionName]
PropName1=PropValue1
port = 8888;
inflag = false ;
}
我只捕获了IOException错误,将IOException改为Exception就可以了,你试试看!
[SETINGS]
IP=192.168.0.1
PORT=5555我的意思是,getProperty()方法有两种,一种是带一个参数的getProperty(String name),另一种是带两个参数的getProperty(String name,String defaultValue),用的是第二个方法,如果能找到文件中的name,则返回name后面的值(假如配置文件如上,假设name为PORT)。问题就处在这:如果找不到sheng.ini文件,则返回defaultValue,如果能找到sheng.ini,但找不到文件内的PORT,则什么都不返回,我需要的是,能否让他在能找到sheng.ini但找不到PORT的情况下也返回defaultValue???盼回复!!!!!!