首页 > 其他 > 详细

IO流(Properties 很重要 实例)

时间:2016-01-24 14:07:23      阅读:142      评论:0      收藏:0      [点我收藏+]
package com.yyq;
import java.io.*;
/*
 * 用于记录应用程序的运行次数
 * 如果使用次数已到,那么给出注册提示
 * 
 * 很容易想到: 计数器
 * 可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增
 * 可是随着该应用程序的退出,该计数器也在内存中消失了
 * 
 * 下次程序启动会先加载计数器的值并加1后再重新存储起来
 * 所以要建立一个配置文件,用于记录该软件的使用次数
     该配置文件使用键值对的形式,这样便于阅读数据
     并操作数据
     键值对数据是map集合
     数据是以文件形式存储,使用IO技术
     
     那么map+io = properties
     配置文件可以实现应用程序数据的共享
 */
// 配置文件 : xml  properties
import java.util.*;
public class PropertiesTest {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Properties prop = new Properties();
        //在操作文件时,养成习惯,先把文件封装成对象
        File file = new File("count.properties");
        // 封装完文件 ,可以对文件进行判断
        if(!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file);
        prop.load(fis);
        // 获取到值
        String value = prop.getProperty("time");
        int count = 0;
        if(value!=null){
            count = Integer.parseInt(value);
            if(count>=5){
                System.out.println("您好,使用次数已到");
            }
        }
        count++;
        prop.setProperty("time",count+"");
        FileOutputStream fos = new FileOutputStream(file);
        prop.store(fos, "");
        fos.close();
        fis.close();
    }

}

 

IO流(Properties 很重要 实例)

原文:http://www.cnblogs.com/yangyongqian/p/5155011.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!