zoukankan      html  css  js  c++  java
  • IO流(Properties 很重要 实例)

    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();
        }
    
    }
  • 相关阅读:
    iOS开发---业务逻辑
    iOS 蓝牙功能 bluetooth
    iOS 企业版 安装失败 原因
    iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途
    iOS 7 修改默认布局从status bar 底部开始
    企业打包时不能安装原因
    UISegmentedControl 功能简单 分析
    ios 推送 证书配置
    ios 获取手机设备信息
    创建quickstart报错
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/5155011.html
Copyright © 2011-2022 走看看