Properties的应用

---恢复内容开始---

对于不确定的数据,需要持久化到硬盘上,就得用到配置文件。

例子:软件试用次数

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTestDemo {

    public static void main(String[] args) throws IOException {

        /*
         * 需求:定义一个功能,记录程序运行的次数,满足5次后,给出提示,试用次数已到,请注册!
         * 思路:
         * 1.需要计数器
         * 2.计数器的值生命周期要比应用程序长,需要对计数器的值进行持久化。
         *     count = 1,里面存储的是键值方式,map集合,要和设备上的数据关联,需要IO技术
         * 集合+IO = Properties
         *
         */
        boolean b = checkCount();
        if(b){
            run();
        }
    }

    public static boolean checkCount() throws IOException {

        boolean isRun = true;

        //1.将配置文件封装成File对象,因为要判断文件是否存在。
        File configFile = new File("tempfile\\count.properties");
        if(!configFile.exists()){//如果不存在就创建一个
            configFile.createNewFile();
        }

        int count=0;//定义计数器

        Properties prop = new Properties();//用于存储配置文件中的数据
        //2.定义流对象
        FileInputStream fis = new FileInputStream(configFile);

        //3.将流中的数据加载到集合中
        prop.load(fis);

        //4.获取键对应的次数
        String value = prop.getProperty("count");

        if(value!=null){
            count = Integer.parseInt(value);
            if(count>=5){
                System.out.println("试用次数已到,请给钱!!");
                isRun=false;
            }
        }
        count++;//对取出的次数进行自增
        //将键count,和自增后的值重新存储到集合中。
        prop.setProperty("count", Integer.toString(count));

        //将集合中的数据存储到配置文件中
        FileOutputStream fos = new FileOutputStream(configFile);

        prop.store(fos, "flush data");

        fos.close();
        fis.close();
        return isRun;
    }

    public static void run(){
        System.out.println("软件运行");
    }

}

---恢复内容结束---

时间: 2024-08-15 23:42:00

Properties的应用的相关文章

springboot的application.properties与.yml的区别

现在我们的application.properties文件内容是: [plain] view plain copy server.port=8090 server.session-timeout=30 server.context-path= server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 spring.datasource.url = jdbc:mysql://localhost:3306/newbirds spring

Properties

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.uti

Spring用代码来读取properties文件

我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

给你的JAVA程序配置参数(Properties的使用)

我们在写JAVA程序时,很多时候运行程序的参数是需要动态改变的 测试时一系列参数,运行时一系列参数 又或者数据库地址也需要配一套参数,以方便今后的动态部署 这些变量的初始化,我们在写小DEMO时完全可以写死在JAVA文件中 但程序需要发布或者局部部署时,这些参数就需要脱离程序代码了 我们有多种存放参数的方式,比如数据库.XML文件又或者直接是txt文件 现在介绍一种使用JAVA,简单方便的参数读取方式 .properties文件,我们并不陌生,很多优秀的框架中就能看到它的存在,比如Hiberna

java 通过 Properties 读取数据库配置 .properties 文件的使用。

system.properties user=root password=root jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8; driverClass=com.mysql.jdbc.Driver 调用方法: public void getProperties(){ Properties prop = new Properties(); try { prop.load(new B

properties文件路径的读取

System.out.println(System.getProperty("user.dir"));  //这个是去工程的绝对路径的  System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));//这个是去当前classpath的uri的!  new Properties().load(new FileInputStream("test.prope

spring 读取properties的两种方法

一:直接使用context命名空间 如: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:websocket="http

Eclipse的properties插件

分享一个不错的编写properties文件的Eclipse插件(plugin),有了它咱们在修改一些简体中文.繁体中文等 Unicode文本时,就不用再运用native2ascii编码了.您能够经过Eclipse中的软件晋级(Software Update)装置此插件,过程如下: 1.打开Eclipse的Help菜单,将鼠标移到Software Update子项,在呈现的子菜单中点击Find and Install:2.在Install/Update对话框中挑选Search for new fe

JAVA使用和操作properties文件

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便.Properties 类存在于包 Java.util 中,该类继承自 Hashtable. 1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索

五种方式让你在java中读取properties文件内容不再是难题

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id