table of Contents
2. The use of Properties as a collection
3. Special features of Properties
4. Combination of Properties and IO
1.Properties overview
Properties belongs to the collection class, which is a collection class that can be used in combination with IO streams.
Properties is a subclass of Hashtable, indicating that it is a Map collection.
2. The use of Properties as a collection
For specific usage, refer to the following code:
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. Special features of Properties
- public Object setProperty(String key,String value): add element
- public String getProperty(String key): Get the element
- public Set<String> stringPropertyNames(): Get a collection of all keys
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. Combination of Properties and IO
- public void load(Reader reader): read the data in the file into the collection
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): Store the data in the collection to a file
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. Demand
Requirement 1: There is a text file, the data is known to be in the form of key-value pairs, but the content is not known. Please write a program to determine whether there is a key such as "lisi", if there is, change its value to "100".
The analysis steps are as follows:
- Load the data in the file into the collection
- Get the collection of all keys, traverse the collection, get each key
- Determine whether there is a key such as "lisi", if there is, modify the value corresponding to the key to "100"
- Restore the data in the collection to the file
code show as below:
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();
}
}
}
}
}
Requirement 2: There is a program for guessing numbers, please write a program that can only be played 5 times in the test class, if more than 5 times, it will prompt: the game trial has ended, please pay!
The program GuessNumber.java of the number guessing game is as follows:
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;
}
}
}
}
Create a file count.txt in the root directory of the project, which is used to save the number of guessing games played several times. The initial value is 0!
count=0
The test classes are as follows:
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();
}
}
}
}
}