//Porperties的应用之一 //判断应用程序的运行次数,例如:某些应用程序只能申请几次机会;

//应用程序的使用期限

//对Properties类,大家还有什么好的应用地方,希望可以互相交流

import java.io.*; import java.util.Properties; public class Propertieslianxi{ public static void main(String [] args) throws Exception{ lianxi(); }
public static void lianxi() throws Exception{ //字节流与文件关联 File file = new File("info.txt"); if(!file.exists()){ file.createNewFile(); } //字节流 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); //创建属性对象 Properties properties = new Properties(); //加载文件内容到集合中 properties.load(bufferedInputStream); //获取使用次数 int time = Integer.parseInt(properties.getProperty("time")); //判断使用次数 if(time>3){ System.out.println("您的使用次数已到,还想继续用,No way!!!"); }
time++; //修改集合内容 properties.setProperty("time",time+"");

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));

//将集合的内容写入文件中 properties.store(bufferedOutputStream,"use time");
//关闭双流 bufferedOutputStream.close(); bufferedInputStream.close();

//使用字符流 /* BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); Properties properties = new Properties(); properties.load(bufferedReader); int time = Integer.parseInt(properties.getProperty("time")); //判断使用次数 if(time>3){ System.out.println("您的使用次数已到,还想继续用,No way!!!"); }
time++; //修改集合内容 properties.setProperty("time",time+"");

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
properties.store(bufferedWriter,"Use Time");

bufferedReader.close(); bufferedWriter.close(); */ } }

posted on 2014-07-15 13:19  001渴望成功  阅读(148)  评论(0)    收藏  举报