黑马程序员 java基础IO FileInputStream

来源:互联网 发布:数据泄露dlp效果 编辑:程序博客网 时间:2024/05/22 00:33

----------- android培训java培训java学习型技术博客、期待与您交流! ------------

 /**
 * 用于记录应用程序运行次数 如果使用次数已到 那么给出注册提示
 * 
 * 很容易想到计数器 可是计数器定义在程序中 随着程序的运行并在程序中存在 并进行自增 可是随着该应用程序退出 该计数器也在内存中消失了 下一次启动计数器
 * 就又重新开始了 从0计数 这样不是我们想要的
 * 
 * 程序即使结束 该计数器的值也存在 下一次程序启动会先加载计数器的值 、并+1后再重新存储起来 所以要建立一个配置文件用于记录该 软件的使用次数
 * 
 * 键值对数据是map集合 数据是以文件形式存储 使用IO技术 map + io---〉properties
 * 
 * 配置文件可以实现应用程序数据共享
 * 
 * @author lazy
 * 
 */
public class RunCount {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


Properties properties = new Properties();


File file;
try {
file = new File("count.ini");
if (!file.exists())
file.createNewFile(); // 没有就创建


FileInputStream fis = new FileInputStream(file);


properties.load(fis);


int count = 0;
String value = properties.getProperty("time");


if (value != null) {
count = Integer.parseInt(value);
if (count >= 5) {
System.out.println("次数到了。。。");
return;
}
}
count++;


properties.setProperty("time", count + "");


FileOutputStream outputStream = new FileOutputStream(file);


properties.store(outputStream, "");


outputStream.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

 

----------------------- android培训java培训java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

0 0
原创粉丝点击