配置文件类 Properties

配置文件类 Properties

主要用于生产配置文件与读取配置文件信息

public class Properties extends Hashtable<Object,Object>
属性列表中每个键及其对应值都是一个字符串。

setProperty方法:
public Object setProperty(String key,String value)
调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
参数:
key - 要置于属性列表中的键。
value - 对应于 key 的值。

load方法:
public void load(Reader reader)
按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。

store方法:
public void store(Writer writer,String comments)
将此 Properties 表中的属性列表(键和元素对)写入输出字符流。
参数:
writer - 输出字符流 writer。
comments - 属性列表的描述。

public void store(OutputStream out,String comments)
将此 Properties 表中的属性列表(键和元素对)写入输出字节流。

getProperty(String key) 
      用指定的键在此属性列表中搜索属性。

注意:
如果配置文件的信息中有中文,则在使用store方法生成配置文件时只能使用字符流,若使用字节流,默认使用的是iso8859-1码表进行编码存储,会乱码

如果Properties中的内容发生了变化,一定要使用Properties重新生成配置文件

package test;

import java.util.*;
import java.util.Map.Entry;
import java.io.*;

public class Demo5 {

    public static void main(String[] args) throws IOException{
        creatProperties();
        readProperties();
    }

    //读取配置文件信息
    public static void readProperties() throws IOException{
        //创建properties对象
        Properties properties=new Properties();
        //加载配置文件信息到Properties中
        properties.load(new FileReader("F:\\test\\persons.properties")); 

        Set<Entry<Object,Object>> entrys=properties.entrySet();
        for(Entry<Object,Object> entry:entrys){
            System.out.println("键: "+entry.getKey()+"  值:"+entry.getValue());
        }

        //修改"dandan"的值
        properties.setProperty("dandan", "000");
        //修改之后重新生成配置文件
        properties.store(new FileWriter("F:\\test\\persons.properties"), "hello");

    }


    //保存配置文件的信息
    public static void creatProperties() throws IOException{
        //创建Properties
        Properties properties=new Properties();
        properties.setProperty("gouwa","123");
        properties.setProperty("dandan","234");
        properties.setProperty("铁蛋","345");

        //遍历
//      Set<Entry<Object,Object>> entrys=properties.entrySet();
//      for(Entry<Object,Object> entry:entrys){
//          System.out.println("键:"+entry.getKey()+"  值:"+entry.getValue());
//      }

        //使用Properties生成配置文件
//      properties.store(new FileOutputStream("F:\\person.properties"), "haha");//public void store(OutputStream out, String comments)  参数:out - 输出流 ,comments - 属性列表的描述。

        //配置文件中有中文时,要用字符流 
        properties.store(new FileWriter("F:\\test\\persons.properties"), "hello");
    }
}
package test;

import java.io.*;
import java.util.*;

/*
 * 需求:实现Properties实现本软件只能运行3次,超过3次之后提示请购买正版,退出jvm
 * 
 */
public class ProTest {

    public static void main(String[] args) throws IOException{
        File file=new File("F:\\test\\count.properties");
        if(!file.exists()){
            //配置文件不存在,则创建配置文件
            file.createNewFile();
        }

        //创建Properties对象
        Properties properties=new Properties();
        //把配置文件加载到properties中
        properties.load(new FileInputStream(file));

        FileOutputStream fileOutputStream=new FileOutputStream(file,true);

        //运行次数
        int count=0;

        //读取配置文件的运行次数
        String value=properties.getProperty("count");
        if(value!=null){
            count=Integer.parseInt(value);
        }

        //判断使用的次数是否已经达到了三次
        if(count==3){
            System.out.println("你已经超出了试用次数,请购买正版软件..");
            System.exit(0);
        }

        count++;
        System.out.println("你已经使用本软件"+count+"次");
        properties.setProperty("count",count+"");
        //使用Properties生成一个配置文件
        properties.store(fileOutputStream, "runtime");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值