public class
Properties
extends Hashtable <K, V>
A Properties object is a Hashtable where the keys and values must be String s . Each property can have a default Properties list which specifies the default values to be used when a given key is not found in this Properties instance.
//装载,读取数据
private void load() {
Properties properties=new Properties();
try {
FileInputStream stream=this.openFileInput("music.cfg");
} catch (FileNotFoundException e) {
return;
}catch(IOException e)
{
return;
}
//保存数据
boolean save() {
Properties properties=new Properties();
//将数据打包成Properties
properties.put("bmusic", String.valueOf(mbMusic));
try {
FileOutputStream stream=this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);// hashtable结构
//将打包好的数据写入文件中
properties.store(stream, "");
}catch(FileNotFoundException e)
{
return false;
}
catch (IOException e) {
return false;
}
return true;
}
本文详细介绍了Java中Properties类的应用,包括如何加载配置文件并读取数据,以及如何将数据保存到配置文件中。通过示例代码展示了处理配置文件的具体步骤,并讨论了在操作过程中可能遇到的异常情况。
1133

被折叠的 条评论
为什么被折叠?



