*.propreties文件是Java中的属性文件,在这种文件里面其文件内容的保存形式为:“key = value ”,通过ResourceBundle可以读取到文件的内容,但是要想编辑文件必须要使用到Propreties类。
通俗点讲就相当于定义一个变量,在这个文件里面定义这些变量的值,在程序里面可以调用这些变量,好处就是,如果程序中的参数值需要变动,直接来改这个.property文件就可以了,不用在去修改源代码。
Propreties类是HashTable的子类,下面通过一个例子熟悉下类Propreties的5个基本操作:
public class PropretiesDemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties properties = new Properties();
properties.setProperty("sn", "陕西");// 1.设置属性
properties.setProperty("sx", "山西");
properties.setProperty("hb", "湖北");
File file = new File("E:\\t.propreties");
properties.store(new FileOutputStream(file), "testPropreties");// 2.将属性存储到文件中
Properties properties_in = new Properties();
properties_in.load(new FileInputStream(file));// 3.从文件中读取属性
System.out.println(properties_in.getProperty("sx"));// 4.取得属性
System.out.println(properties_in.getProperty("sh", "未知地名"));//5.取得属性,如果为空则返回传入的默认值
}
}