目录
1.Properties概述
Properties属于集合类,是一个可以和IO流相结合使用的集合类。
Properties是Hashtable的子类,说明是一个Map集合。
2.Properties作为集合的使用
具体用法,参考如下代码:
package com.hw.properties;
import java.util.Properties;
import java.util.Set;
/**
* Properties作为集合的使用
*
* @author HW
*
*/
public class PropertiesDemo {
public static void main(String[] args) {
// 创建集合对象
Properties props = new Properties();
// 添加元素
props.put("username", "kiki");
props.put("password", "123456");
props.put("email", "[email protected]");
// 获取所有键的集合
Set<Object> set = props.keySet();
for (Object key : set) {
Object value = props.get(key);
System.out.println(key + "---" + value);
}
}
}
3.Properties的特殊功能
- public Object setProperty(String key,String value):添加元素
- public String getProperty(String key):获取元素
- public Set<String> stringPropertyNames():获取所有的键的集合
package com.hw.properties;
import java.util.Properties;
import java.util.Set;
/**
* Properties的特殊功能
*
* @author HW
*
*/
public class PropertiesDemo2 {
public static void main(String[] args) {
// 创建集合对象
Properties props = new Properties();
// 添加元素
props.setProperty("username", "nono");
props.setProperty("password", "123456");
props.setProperty("email", "[email protected]");
// 获取所有键的集合
Set<String> set = props.stringPropertyNames();
for (String key : set) {
// 根据键获取值
String value = props.getProperty(key);
System.out.println(key + "---" + value);
}
}
}
4.Properties和IO的结合使用
- public void load(Reader reader):把文件中的数据读取到集合中
package com.hw.properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Properties和IO的结合使用
*
* public void load(Reader reader):把文件中的数据读取到集合中
*
* @author HW
*
*/
public class PropertiesDemo3 {
public static void main(String[] args) {
// 创建集合对象
Properties props = new Properties();
FileReader fr = null;
try {
// 创建字符输入流对象
fr = new FileReader("test2.properties");
// 把文件中的数据读取到集合中
props.load(fr);
// System.out.println(props);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- public void store(Writer writer,String comments):把集合中的数据存储到文件
package com.hw.properties;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
/**
* Properties和IO的结合使用
*
* public void store(Writer writer,String comments):把集合中的数据存储到文件
*
* @author HW
*
*/
public class PropertiesDemo4 {
public static void main(String[] args) {
Properties props = null;
FileWriter fw = null;
// 创建集合对象
props = new Properties();
// 添加元素
props.setProperty("username", "kikinono");
props.setProperty("password", "123456");
try {
// 创建字符输出流对象
fw = new FileWriter("fw.txt");
// 把集合中的数据存储到文件
props.store(fw, "userInfo");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.需求
需求1:有一个文本文件,已知数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”。
分析步骤如下:
- 把文件中的数据加载到集合中
- 获取所有键的集合,遍历集合,获取得到每一个键
- 判断是否有"lisi"这样的键存在,如果有就将键对应的值修改为"100"
- 把集合中的数据重新存储到文件中
代码如下:
package com.hw.properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* 需求1:我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。
* 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”
* @author HW
*
*/
public class PropertiesTest {
public static void main(String[] args) {
Properties props = null;
FileReader fr = null;
FileWriter fw = null;
try {
// 创建字符输入流对象
fr = new FileReader("user.properties");
// 创建集合对象
props = new Properties();
// 把文件中的数据读取到集合中
props.load(fr);
// 获取所有键的集合
Set<String> set = props.stringPropertyNames();
for (String key : set) {
if(key.equals("lisi")) {
props.setProperty(key, "100");
break;
}
}
// 把集合中的数据重新存储到文件中
fw = new FileWriter("user.properties");
props.store(fw, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需求2:有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能玩5次,超过5次则提示:游戏试玩已结束,请付费!
猜数字小游戏的程序GuessNumber.java如下:
package com.hw.properties;
import java.util.Scanner;
/**
* 猜数字小游戏程序
*
* @author HW
*
*/
public class GuessNumber {
public static void start() {
// 产生一个随机数
int number = (int) (Math.random() * 100) + 1;
// 定义一个统计变量
int count = 0;
while (true) {
// 键盘录入一个数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据(1-100):");
int guessNumber = sc.nextInt();
count++;
// 判断
if (guessNumber > number) {
System.out.println("你猜的数据" + guessNumber + "大了");
} else if (guessNumber < number) {
System.out.println("你猜的数据" + guessNumber + "小了");
} else {
System.out.println("恭喜你," + count + "次就猜中了");
break;
}
}
}
}
在项目的根目录下创建一个文件count.txt,用来保存猜数字小游戏总共玩了几次,初始值为0!
count=0
测试类如下:
package com.hw.properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
/**
* 需求2:有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次则提示:游戏试玩已结束,请付费!
*
* @author HW
*
*/
public class PropertiesTest2 {
public static void main(String[] args) {
Properties props = null;
FileReader fr = null;
FileWriter fw = null;
try {
// 创建字符输入流对象
fr = new FileReader("count.txt");
// 创建Properties集合
props = new Properties();
// 把count.txt文件中数据加载到集合中
props.load(fr);
// 获取count.txt文件中的count值
String value = props.getProperty("count");
int number = Integer.parseInt(value);
if (number >= 5) {
System.out.println("游戏试玩已结束,请付费。");
System.exit(0);
} else {
number++;
props.setProperty("count", String.valueOf(number));
// 创建字符输出流对象
fw = new FileWriter("count.txt");
// 把count值重新存储到count.txt文件中
props.store(fw, null);
GuessNumber.start();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}