今天学习的内容是Properties类
Properties类已经见过两次了,实际上它是一个属性集合,主要用于结合IO技术操作配置文件。Properties类的特点是:
- 集合中的键值对都是字符串
- 集合中的数据可以保存到流中,或者从流中获取
- 可以被持久化(也就是持久化到配置文件中)
顺便说一下配置文件:配置文件在实际开发中十分重要,用于配置应用程序所需的环境信息。配置简单信息通常使用.ini或者.properties格式的配置文件(例如System.ini和Tomcat中的catalina.properties),这种配置文件内部存储的是“键=值”形式的配置信息;而配置复杂环境信息就要使用XML配置文件(框架中会大量使用这种配置文件!),这种配置文件内部存储的是标签形式的配置信息
关于Properties类的常用操作如下例:
public class Test85 {
public static void main(String[] args) throws IOException {
propertiesTest_1();
propertiesTest_2();
propertiesTest_3();
propertiesTest_4();
propertiesTest_5();
}
//对已有配置文件中的信息进行修改
private static void propertiesTest_5() throws IOException {
Properties prop = new Properties();
File file = new File("info.txt");
if(!file.exists()){
file.createNewFile();
}
prop.load(new FileInputStream(file));
prop.setProperty("1", "k");
prop.list(System.out);
prop.store(new FileOutputStream(file), "modify");
prop.list(System.out);
}
//Properties类的load()方法
private static void propertiesTest_4() throws IOException {
//集合中的数据来自于一个文件,需要关联输入流,注意必须保证该文件中的数据是键值对
Properties prop = new Properties();
prop.load(new FileInputStream("info.txt"));
prop.list(System.out);
}
//Properties类的store()方法
private static void propertiesTest_3() throws IOException{
//想要将这些集合中的键值对信息持久化存储到文件中,需要关联输出流
Properties prop = new Properties();
prop.setProperty("1", "a");
prop.setProperty("2", "b");
prop.setProperty("3", "c");
prop.setProperty("4", "e");
prop.setProperty("4", "d");
prop.store(new FileOutputStream("info.txt"), "int+abc");// 文件描述最好不要使用中文
}
// Properties类的list()方法
private static void propertiesTest_2() {
Properties prop = System.getProperties();
prop.list(System.out);// 将出行列表输出到指定输出流(该方法只是用来调试)
}
// Properties类基本操作
private static void propertiesTest_1() {
Properties prop = new Properties();
prop.setProperty("1", "a");
prop.setProperty("2", "b");
prop.setProperty("3", "c");
prop.setProperty("4", "e");
prop.setProperty("4", "d");
Set<String> keySet = prop.stringPropertyNames();
for (String key : keySet) {
String value = prop.getProperty(key);
System.out.println(key + ":" + value);
}
}
}
若希望使用Properties类和IO技术实现功能:获取程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不再运行该程序。由于程序运行结束,作为计数器的变量将会消失,下一次启动程序将会重新初始化计数器,所以要将程序运行次数持久化到配置文件中,下例使用.properties格式的配置文件完成上述功能:
public class Test86 {
public static void main(String[] args) {
getAppCount();
}
private static void getAppCount() {
// 创建配置文件
File confile = new File("count.properties");// 注意Windows常用.ini格式的配置文件
// Java中则是.properties格式的配置文件
try {
if (!confile.exists()) {
confile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
// 创建Properties集合
Properties prop = new Properties();
// 创建输入流,读取配置文件中的内容
FileInputStream fis = null;
try {
fis = new FileInputStream(confile);
prop.load(fis);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 从集合中通过键获取次数
String value = prop.getProperty("time");
int count = 0;// 计数器
if (value != null) {
count = Integer.parseInt(value);
if (count >= 5) {
throw new RuntimeException("使用次数已到,请注册!");
}
}
count++;
System.out.println("第"+count+"次运行程序");
// 将改变后的次数重新存储到集合中
prop.setProperty("time", Integer.toString(count));
// 将集合中的内容写到配置文件中
FileOutputStream fos = null;
try {
fos = new FileOutputStream(confile);
prop.store(fos, "jishuqi");;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}