黑马程序员技术交流社区
标题:
Properties 创建一个配置文件 限制程序运行次数
[打印本页]
作者:
fmi110
时间:
2015-8-8 09:37
标题:
Properties 创建一个配置文件 限制程序运行次数
Properties
/*
需求:创建一个配置文件,当程序运行到达5次时,不允许再运行
分析: 1、配置文件需要长期保存在硬盘,所以需要文件流
2、需要读取文件使用次数,所以考虑使用集合
3、集合和IO流结合,所以使用Properties
*/
import java.io.*;
import java.util.*;
class PropertiesTest
{
public static void main(String[] args) throws IOException
{
//创建抽象文件目录,并判断配置文件是否已存在,若不存在则新建一个,
//已存在则读取存入Properties
File file = new File("count.ini");
//判断
if(!file.exists())
file.createNewFile();
//创建流,读取配置文件
FileInputStream fin = new FileInputStream(file);
//将配置信息 存至集合Properties
Properties prop = new Properties();
//加载信息
prop.load(fin);
int count = 0 ;
String value = prop.getProperty("time");
if(value!=null){
count = Integer.parseInt(value);
if(count>=5)
{
System.out.println("该交钱啦啦啦啦啦");
return ;
}
}
count++;
prop.setProperty("time",String.valueOf(count));
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos,"");
fos.close();
fin.close();
System.out.println("Hello World!");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2