properties集合的简单例子

package com.lxb.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.Set;


public class PropertiesDemo {


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


//存储数据
propertiedDemo();
//list方法
propertiedDemo2();
//store方法
propertiedDemo3();
//读取配置文件并修改信息
propertiedDemo4();
//获取一个应用程序运行的次数,超过5次,给出使用次数已到请注册的请求
propertiedDemo5();
}


private static void propertiedDemo5() throws IOException {
// TODO Auto-generated method stub
/**
* 思路:
* 1:应该有计数器
* 每次程序启动都需要计数一次,并且是在原有的次数上进行计数
* 2:计数器就是一个变量
* 3:如何使用计数器
* 程序启动时,应该先读取这个用于记录计数器信息的配置文件
* 获取上一次技术次数,并进行使用次数的判断
* 其次,对该次数进行自增,并将自增后的次数重新存储到配置文件中
* 4:文件中的信息该如何进行存储体现
* 直接存储次数可以,但是不明确该数据的含义,所以起名字就变得很重要、
* 这就有了名字和值的对应,所以可以使用键值对
* 可以映射关系map集合搞定,有需要读取硬盘上的数据,所以map+io=properties
*/
File f=new File("e:\\count.properties");
if (!f.exists()) {
f.createNewFile();
}

Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);

if(prop.getProperty("count")==null) {
prop.setProperty("count", "1");
FileOutputStream fos=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "count");
fos.close();
osw.close();
}else {
Integer count=Integer.valueOf(prop.get("count").toString());
if(count==5) {
System.out.println("去注册吧");
}else {
count++;
prop.setProperty("count", count+"");
FileOutputStream fw=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "count");
fw.close();
osw.close();
}
}

}


private static void propertiedDemo4() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream("e:\\demo.txt");
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);

//对已有的配置文件中的信息进行修改
prop.setProperty("03", "赵六");
FileOutputStream fw=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "num+name");
fw.close();
osw.close();

}


private static void propertiedDemo3() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
//持久化存储到文件中,需要关联输出流
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");

FileOutputStream fos=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "num+name");
fos.close();
}


private static void propertiedDemo2() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop=System.getProperties();
prop.list(System.out);
}


private static void propertiedDemo() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");
//取出所有元素
Set<String> set=prop.stringPropertyNames();
for (String string : set) {
System.out.println(string+":"+prop.getProperty(string));
}
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值