接下来将学习的对象是 Map中HashMap的子类,但它跟文件的读取密切相关,因此放到这里讲解:
那么他到底有啥特点呢:
Properties:
*1, Map接口中Hashtable的子类。
*2, 该类上没有泛型定义,因为它的键值都是固定的字符串类型。
*3, 因为存储都是字符串数据,通常都作为属性信息存在。
*4, 该集合最大的特点就是可以和IO技术相结合。也就是,该集合中的数据可以来自于流。也可以将集合中的数据写入到流中。
该对象最主要的用途:可以用于键值对形式的配置文件。用于持久化存储
下面我们着重看一下使用方法:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
loadDemo();
}
public static void loadDemo() throws IOException{
Properties prop = new Properties();
BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
String line = null;
while((line=bufr.readLine())!=null){
String[] strs = line.split("=");
prop.setProperty(strs[0], strs[1]);
}
bufr.close();
prop.list(System.out);
}
public static void methodDemo_3() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info.txt");
//将流中的数据存储到集合中。 使用集合的load方法。
prop.load(fis);
//想要对数据做修改。
prop.setProperty("wangcai", "37");
//想要将修改的数据保存到文件中。
// 使用store方法。
FileOutputStream fos = new FileOutputStream("info.txt");
prop.store(fos, "haha");
fos.close();
fis.close();
prop.list(System.out);
}
//演示和IO技术相结合的方法。
public static void methodDemo_2() {
Properties prop = new Properties();
prop.setProperty("wangwu", "28");
prop.setProperty("zhangsan", "20");
prop.setProperty("lisi", "34");
//使用list方法将集合中的数据指定目的地。
prop.list(System.out);//一般用于调试。
}
//对Properties集合最基本演示。
public static void methodDemo_1(){
//1,创建Properties集合对象。
Properties prop = new Properties();
//2,存储元素。
prop.setProperty("wangwu", "28");
prop.setProperty("zhangsan", "20");
prop.setProperty("lisi", "34");
//取元素。所有。
Set<String> nameSet = prop.stringPropertyNames();
for(String name : nameSet){
String value = prop.getProperty(name);
System.out.println(name+":"+value);
}
}
}
结合上述演示,我们做一个练习:
定义一个功能,用于记录住软件运行次数,如果运行次数大于5次,不要再运行,并给出提示:
试用次数已懂,请注册!给钱!
思路:
1. 计数器。而且这个计数器必须软件运行结束后,持久化存储。
2. 每次软件启动都要读取这个记录了 计数器 的数据的文件。并将
计数器的值取出后 自增后,再重新存储。
3. 数值需要名称标记,就有了键值对。这就是map集合,还要操作硬盘设备上
的数据就使用了IO流 和 map 结合的对象,正好有 Properties。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) throws IOException {
if(countDemo()){
//运行程序代码。
System.out.println("运行程序");
}else{
System.out.println("试用次数已到,请注册!给钱!");
}
}
public static boolean countDemo() throws IOException{
Properties prop = new Properties();
int count = 0;
//配置文件。
File confile = new File("config.txt");
if(!confile.exists())
confile.createNewFile();
FileInputStream fis = new FileInputStream(confile);
//将流中的数据加载到prop中。
prop.load(fis);
//获取配置文件中的次数。
String value = prop.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
return false;
}
}
count++;
System.out.println("运行"+count+"次");
//将具体的键和次数存储到集合中。
prop.setProperty("count", String.valueOf(count));//count+"";
//将集合中的数据写入到文件中持久化。
FileOutputStream fos = new FileOutputStream(confile);
prop.store(fos, "");
fos.close();
fis.close();
return true;
}
}
至此IO体系中的大半壁江山,已经学习完毕,接下来,主要学习的是:
IO 体系中的功能流对象!