您的位置:首页 > 编程语言 > Java开发

java:Properties类的相关知识

2015-07-25 15:29 465 查看
Properties类是HashTable的子类,存放的数据以键值对的形式存在

/*
记录程序运行次数的配置文件
*/
import java.io.*;
import java.util.*;

class TestProperties
{
public static void main(String[] args) throws Exception
{
//关联配置文件
File file = new File("D:\\info.txt");
if(!file.exists())
{
file.createNewFile();
}

FileInputStream fis = new FileInputStream(file);

//把流加载到集合中
Properties pro = new Properties();
pro.load(fis);

int count = 0;
String value = pro.getProperty("time");  //根据键获取值
if(value != null)
{
count = Integer.parseInt(value);
if(count >= 4)
{
System.out.println("使用次数已达五次");
return;
}
}
count ++;

//把键值对存放到集合中
pro.setProperty("time", count +"");

//把内存中的数据存入文件中
FileOutputStream fos = new FileOutputStream("D:\\info.txt");
pro.store(fos, "");

fis.close();
fos.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: