我正在使用 Eclipse,并且在 mi src 文件夹之外创建了一个 res 文件夹。在其中,我创建了一个名为“config.cfg”的文本文件。看起来像这样:
# System configuration
# Comments will automatically be excluded by the program
radiomodemPort=20001
sisnetPort=5562
sisnetHost=213.229.135.3
sisnetUser=jogg
sisnetPass=jogg
为读取它而编写的代码不起作用:它不会加载任何存储的变量。我的代码是:
private String sisnetHost;
private int sisnetPort;
private int radiomodemPort;
private String sisnetUser;
private String sisnetPass;
private boolean sisnetHostLoaded;
private boolean sisnetPortLoaded;
private boolean radiomodemPortLoaded;
private boolean sisnetUserLoaded;
private boolean sisnetPassLoaded;
public boolean getSettingsFromFile(){
Properties config = new Properties();
try {
config.load(new FileInputStream("res/config.cfg"));
Enumeration<Object> en = config.keys();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
if(key.equals(sisnetHost)){
sisnetHost = (String)config.get(key);
sisnetHostLoaded = true;
}
if(key.equals(sisnetPort)){
sisnetPort = (Integer)config.get(key);
sisnetPortLoaded = true;
}
if(key.equals(sisnetUser)){
sisnetUser = (String)config.get(key);
sisnetUserLoaded = true;
}
if(key.equals(sisnetPass)){
sisnetPass = (String)config.get(key);
sisnetPassLoaded = true;
}
if(key.equals(radiomodemPort)){
radiomodemPort = (Integer)config.get(key);
radiomodemPortLoaded = true;
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return false;
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
if(!(sisnetHostLoaded && sisnetPortLoaded && sisnetUserLoaded && sisnetPassLoaded && radiomodemPortLoaded))
fillUnloadedSettings();
return true;
}
怎么了?