玩转Properties

/*
 * 玩转Properties,只要熟悉这5个方法:
 * new Properties(), load(), getProperty() , 
 * setProperty(), store()
 */


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import org.junit.Test;

public class PropertiseDemo {
	/*
	 * 玩转Properties,只要熟悉这5个方法:
	 * new Properties(), load(), getProperty() , 
	 * setProperty(), store()
	 */
	@Test //读取配置文件中的信息
    public void demo1() throws Exception{
		Properties p=new Properties();
		p.load(new FileInputStream("a.properties"));//io的起始位置: 项目根目录---和src、bin并列的位置
		String size=p.getProperty("size");
		System.out.println("size:"+size);
		 
		String aa = p.getProperty("aa", "30"); //若配置文件中不存在该key,则返回值为我们给的默认值"30"
		System.out.println("aa:"+aa);
		
		System.out.println( p.getProperty("2") );
		
		p.setProperty("age", "24");
		p.store(new FileOutputStream("a.properties"),null);
	} 
	
	@Test //存储配置信息
    public void demo2() throws Exception{
		Properties p = new Properties();//内存中的对象
		p.setProperty("aa", "23");
		p.setProperty("bb", "33");
		p.setProperty("12", "cc");
		p.store(new FileWriter("b.properties"), "this is comment,Over!");
		
	}
	
	@Test //Java中把虚拟机的配置信息也封装成Properties对象,供我们使用
	public void demo3(){
		//罗列出系统(Java虚拟机)的属性
		Properties p=System.getProperties(); 
		Enumeration e=p.propertyNames();
		while( e.hasMoreElements() ){
		   String k = (String)e.nextElement(); //返回的是Object,要强制造型
		   String v = p.getProperty(k);
		   System.out.println( "k="+k+",v=="+v );
		}
		
		//System.getProperty("line.separator");//输出当前系统的换行符
	}
}
mport java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JOptionPane;

public class PropertiesSoftTryTimes {

	public static void main(String[] args) {
		if(validate()){
			System.out.println("执行软件的正常功能....");
			module();
		}else{
			JOptionPane.showMessageDialog(null, "软件试用次数已到,请注册!");
		}
	}
	public static void module(){
		System.out.println("软件模块执行.....");
	}
	public static boolean validate(){
		 try{
		 Properties p=new Properties();
		 File configFile=new File("config.cfg");
		 if(!configFile.exists()){
			 configFile.createNewFile();
		 }
		//经过上面的防护,一定存在配置文件:config.cfg
			
		//该句放此处是bug:  FileOutputStream out = new FileOutputStream(configFile);
		
		 p.load(new FileInputStream(configFile));
		 
		 String strValue =p.getProperty("count","0");
		 int count=0;
		 try{
		 count=Integer.parseInt(strValue);
		 } catch (NumberFormatException e) {
				e.printStackTrace(); //软件毁坏,TODO:提示并退出软件
		    }
		 if(count>=5){
				return false;
			}
		 
		//程序能运行到这儿,说明次数未到
		 count++;
		 JOptionPane.showMessageDialog(null, "软件已经试用"+count+"次!");
		
		//把更新后的次数持久化
		p.setProperty("count", ""+count);
		FileOutputStream out = new FileOutputStream(configFile);//该句每次会创建一个新的文件
		p.store(out, null);
		
		 } catch (IOException e) {
				e.printStackTrace();
			}			
		return true;
	 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值