Properties本属于Map接口下的一个子类,由于涉及到IO流,所以这里介绍一下
Properties是HashTable下的一个子类
Properties集合:
特点:
- 该集合中的键和值都是字符串类型
集合中的数据可以保存到流中,或者从流获取。
通常该集合用于操作以键值对形式存在的配置文件。
比如打开了PyCharm的settings配置文件可以看到键值对都是字符串的形式
常用方法
Object setProperty(String key, String value)
//调用 Hashtable 的方法 put。往Map中存
String getProperty(String key)
//用指定的键在此属性列表中搜索属性。由key获取value
Set<String> stringPropertyNames()
//返回此属性列表中的键集,其中该键及其对应值是字符串
程序演示:
public static void main(String[] args) throws IOException {
propertiesDemo();
}
/*
* Properties集合的存和取。
*/
public static void propertiesDemo(){
//创建一个Properties集合。
Properties prop = new Properties();
//存储元素。
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","31");
prop.setProperty("wangwu","36");
prop.setProperty("zhaoliu","20");
//修改元素。
prop.setProperty("wangwu","26");
//取出所有元素。
Set<String> names = prop.stringPropertyNames();
for(String name : names){
String value = prop.getProperty(name);
System.out.println(name+":"+value);
}
}
输出结果:
- 演示Properties集合和流对象相结合的功能。
public static void main(String[] args) throws IOException {
propertiesDemo();
}
public static void propertiesDemo(){
Properties prop = new Properties();
//存储元素。
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","31");
prop.setProperty("wangwu","36");
prop.setProperty("zhaoliu","20");
//将prop的内容显示
prop.list(System.out);
}
输出结果:list()方法将属性列表输出到指定的输出流。
这里是将Properties中的内容打印到控制台上,以便时刻观察集合中的内容
这个方法常用于测试中
- Properties集合的store方法
将集合中的字符串信息保存起来以便下次使用:保存在硬盘中(file)实体化。
想要将这些集合中的字符串键值信息持久化存储到文件中,需要关联输出流。
public static void main(String[] args) throws IOException {
propertiesDemo();
}
public static void propertiesDemo() throws IOException {
Properties prop = new Properties();
//存储元素。
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","31");
prop.setProperty("wangwu","36");
prop.setProperty("zhaoliu","20");
//不同设备对应不同的文件类型
//硬盘:file
//键盘、控制台:System.in,System.out
//网络:Socket
//由于要存储在硬盘上所以使用file,而且输出的字节流,所以用FileOutputStream
FileOutputStream fos = new FileOutputStream("info.txt");
//将集合中数据存储到文件中,使用store方法。
prop.store(fos, "info");
fos.close();
}
程序输出结果:
在eclipse的workshop对应的文件夹下产生了info.txt
info.txt中的信息如下所示:
store方法中的第二个参数对应的就是txt中的第一行
- 集合中的数据来自于一个文件
方法展示:
集合中的数据来自于一个文件,注意!必须要保证该文件中的数据是键值对,需要使用到读取流。
public static void main(String[] args) throws IOException {
propertiesDemo();
}
public static void propertiesDemo() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info.txt");
//使用load方法。
prop.load(fis);
prop.list(System.out);
}
程序结果:
读取的是上一个程序展示中的info.txt
- 如果自己实现load方法,思路是从info.txt中读取一行,然后以“=”切分字符串,左边的作为key,右边的作为value存入到Properties集合中
模拟一下load方法(此模拟方法作为理解展示)
public static void myLoad() throws IOException{
//创建一个Properties对象来存储
Properties prop = new Properties();
//使用缓冲器中的readLine方法
BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
String line = null;
while((line=bufr.readLine())!=null){
//跳过注释
if(line.startsWith("#"))
continue;
//将切分后的字符串存入
String[] arr = line.split("=");
prop.setProperty(arr[0], arr[1]);
}
//打印到控制台上
prop.list(System.out);
bufr.close();
}
- 对已有的配置文件中的信息进行修改。
思路:
读取这个文件,并将这个文件中的键值数据存储到集合中,在通过集合对数据进行修改,在通过流将修改后的数据存储到文件中。
public static void test() throws IOException{
//读取这个文件。
File file = new File("info.txt");
if(!file.exists()){
file.createNewFile();
}
FileReader fr = new FileReader(file);
//创建集合存储配置信息。
Properties prop = new Properties();
//将流中信息存储到集合中。
prop.load(fr);
prop.setProperty("wangwu", "16");
//修改后用输出流将数据写到硬盘上的文件中
FileWriter fw = new FileWriter(file);
//存储
prop.store(fw,"");
fw.close();
fr.close();
}
FileWriter fw = new FileWriter(file);这个语句的位置不可以放在上面,因为放在上面与file关联后,就会新创建一个新文件将原来的info.txt覆盖。这时候程序在执行到load(fr)时读到的是一个空文件。所有的东西都改完了才需要输出流的动作,所以建议将输出流语句放在后面!
程序输出:
此图为原始info.txt
此图为程序运行后的结果
对应的info.txt
最后看一个关于Properties集合的练习,加深一下对这个部分的理解
需求:获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示。并不要在运行程序。
思路:
- 应该有计数器,每次程序启动都需要计数一次,并且在原有的次数上进行计数。
- 计数器就是一个变量。程序启动时候进行计数,计数器必须存在于内存并进行运算,可是程序一结束,计数器消失了。那么再次启动该程序,计数器又重新被初始化了,而我们需要多次启动同一个应用程序,使用的是同一个计数器,这就需要计数器的生命周期变长,从内存存储到硬盘文件中。
- 如何使用这个计数器呢?首先,程序启动时,应该先读取这个用于记录计数器信息的配置文件,获取上一次计数器次数,并进行试用次数的判断,其次,对该次数进行自增,并自增后的次数重新存储到配置文件中。
- 文件中的信息该如何进行存储并体现。直接存储次数值可以,但是不明确该数据的含义。 所以起名字就变得很重要。这就有了名字和值的对应,所以可以使用键值对。可是映射关系map集合搞定,又需要读取硬盘上的数据,所以map+io = Properties.
public class PropertiesTest {
public static void main(String[] args) throws IOException {
getAppCount();
}
public static void getAppCount() throws IOException{
//将配置文件封装成File对象。
File confile = new File("count.properties");
//文件的健壮性判断
if(!confile.exists()){
confile.createNewFile();
//如果指定的文件不存在并成功地创建,则返回 true;如果指定的文件已经存在,则返回 false
}
FileInputStream fis = new FileInputStream(confile);
Properties prop = new Properties();
prop.load(fis);
//从集合中通过键获取次数。
String value = prop.getProperty("time");
//定义计数器。记录获取到的次数。
int count =0;
if(value!=null){
count = Integer.parseInt(value);
//将字符串参数作为有符号的十进制整数进行解析。
if(count>=5){
// System.out.println("使用次数已到,请注册,给钱!");
// return;
throw new RuntimeException("使用次数已到,请注册,给钱!");
}
}
count++;
//将改变后的次数重新存储到集合中。
prop.setProperty("time", count+"");
//或者可以写成prop.setProperty("time", Integer.toString(count));
FileOutputStream fos = new FileOutputStream(confile);
prop.store(fos, "");
fos.close();
fis.close();
}
}
程序运行一次时
当程序运行到5次时
继续运行到第6次时
这个代码块可以封装起来加在自己写的软件中,其实在使用的各种软件中都存在各式各样的配置文件,里面存的都是相应的键值对。每次操作软件自行写入,在下次运行时首先读取配置文件。在Java中简单的配置文件使用的.properties,复杂的配置文件使用的是.xml。
比如.properties使用键值对:
name = zhangsan
age = 20
name2 = lisi
age2 = 22
.xml使用的是标签的形式简洁
<Person id = '007'>
<name>zhangsan</name>
<age>20</age>
</Person>
<Person>
<name>lisi</name>
<age>22</age>
</Person>
本人是菜鸟一枚,当做学习笔记写博客。谢谢各路大咖驻足审阅