Properties介绍
- 是一个Map体系的集合类
- Properties可以保存到流中或从流中加载
- 属性列表中的每个键及其对应的值都是一个字符串
public class PropertiesDemo01 {
public static void main(String[] args) {
//创建集合对象
Properties prop=new Properties();
//存储元素
prop.put("itheima001","林青霞");
prop.put("itheima002","张曼玉");
prop.put("itheima003","王祖贤");
//遍历集合
Set<Object> keySet = prop.keySet();
for(Object key:keySet){
Object value = prop.get(key);
System.out.println(key+","+value);
}
}
}
Properties作为Map集合的特有方法
public class PropertiesDemo02 {
public static void main(String[] args) {
Properties prop=new Properties();
prop.setProperty("itheima001","林青霞");
prop.setProperty("itheima002","王祖贤");
System.out.println(prop.getProperty("itheima001"));
System.out.println(prop);
Set<String> names = prop.stringPropertyNames();
for(String key:names){
String value = prop.getProperty(key);
System.out.println(key+","+value);
}
}
}
Properties和IO流相结合的方法
public class PropertiesDemo003 {
public static void main(String[] args) throws IOException{
//把集合中的数据保存到文件
myStore();
//把文件中的数据加载到集合
myLoad();
}
private static void myLoad() throws IOException{
Properties prop=new Properties();
FileReader fr=new FileReader("D:\\JAVA\\myOtherStream\\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop=new Properties();
prop.setProperty("itheima001","林青霞");
prop.setProperty("itheima002","张曼玉");
prop.setProperty("itheima003","王祖贤");
FileWriter fw=new FileWriter("D:\\JAVA\\myOtherStream\\fw.txt");
prop.store(fw,null);
fw.close();
}
}
游戏次数案例
需求:实现猜数字小游戏只能试玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
- 分析步骤
- 写一个游戏类,里面有一个猜数字的小游戏
- 写一个测试类,测试类中有main()方法,main()方法中写如下代码:
A:从文件中读取数据到Properties集合,用load()方法实现
文件已经存在:game.txt
里面有一个数据值:count=0
B:通过Properties集合获取到玩游戏的次数
C:判断次数是否到到3次了
如果到了,给出提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
如果不到3次:
次数+1,重新写回文件,用Properties的store()方法实现玩游戏
public class GameDemo {
public static void main(String[] args) throws IOException {
//从文件中读取数据到Properties集合,用load()方法实现
Properties prop=new Properties();
FileReader fr=new FileReader("D:\\JAVA\\myOtherStream\\game.txt");
prop.load(fr);
fr.close();
//通过Properties集合获取到玩游戏的次数
String count = prop.getProperty("count");
int number = Integer.parseInt(count);
if(number>=3){
System.out.println("游戏结束");
}else{
GuessNumber.start();
//次数+1,重新写回文件,用Properties的store()方法实现
number++;
prop.setProperty("count",String.valueOf(number));
FileWriter fw=new FileWriter("D:\\JAVA\\myOtherStream\\game.txt");
prop.store(fw,null);
fw.close();
}
}
}
public class GuessNumber {
public GuessNumber() {
}
public static void start(){
Random r=new Random();
int number=r.nextInt(100)+1;
while (true){
//使用程序实现猜数字,每次均要输入猜测的数字值,需要使用键盘录入实现
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要猜的数字:");
int guessNumber=sc.nextInt();
//比较输入的数字和系统产生的数据,需要使用分支语句,这里使用if...else..if..格式,根据不同情况进行猜测结果显示
if(guessNumber>number){
System.out.println("你猜的数字大了");
}else if(guessNumber<number){
System.out.println("你猜的数字小了");
}else {
System.out.println("恭喜你猜中了");
break;
}
}
}
}