Properties集合其实是继承了HashTable,但HashTable经过HashMap的迭代后就被废除了,不过由于Properties的功能十分强大,经常在反射机制以及框架中被使用,因此到目前它还依旧活跃在世界的舞台上
现在开始讲解它的用法
Properties extends HashTable<K,V> implements Map<K,V>
一.由于Properties继承了Hash,所以也是使用键值配对存放内容,key和value默认都是字符串
二.Properties文件的后缀名是 .properties,保存的格式为

三.Properties是一个唯一和IO相关的集合
1.可以使用store方法 把集合中的临时数据持久化写入到硬盘中去
2.可以使用load方法 把硬盘中保存的文件(键值对),读取到集合中使用
四.常见方法
1.Object setProperty(String key,String value),底层调用了HashTable的put方法, 因此该方法相当于put方法
2.String getProperty(String key) 通过key找到value值,该方法相当于Map的get方法
3.Set stringPropertyNames()返回次属性列表中的键值,相当于Map中的keySet方法
测试01:
Properties properties = new Properties();
properties.setProperty("小郑","秃头");
properties.setProperty("三刀流","索隆");
properties.setProperty("程序员","小郑");
Set<String> set = properties.stringPropertyNames();
for (String str: set){
System.out.println("key = " + str + "; value = " + properties.getProperty(str));
}
/*
key = 程序员; value = 小郑
key = 小郑; value = 秃头
key = 三刀流; value = 索隆
*/
//输出结果为乱序,可知为hash存放
测试02:
1.void store(OutputStream out, String comments)
2.void store(Writer writer, String comments)
俩个方法的比较:
1方法是字节输出流,不能写入中文(写中文会产生乱码)
2方法是字符输出流,可以写中文
String comments:注释, 用来解释说明保存的文件是什么做的,默认是Unicode编码,不过一般这里直接用“ ”空字符串
使用步骤:
1.创建Properties集合,添加数据
2.创建使用字节输出流/字符输出流,构造方法中绑定要输出的目的地
3.使用Properties集合中的store方法,把集合中的临时数据,持久化写入到硬盘中存储
4.释放资源
代码01:store-stream
Properties properties = new Properties();
properties.setProperty("小郑","秃头");
properties.setProperty("三刀流","索隆");
properties.setProperty("程序员","小郑");
FileOutputStream stream = new FileOutputStream(path);
properties.store(stream,"测试");
stream.close();
运行结果:文件里面的内容(用字节流写字符出现乱码的实例)

代码02:store字符
Properties properties = new Properties();
properties.setProperty("小郑","秃头");
properties.setProperty("三刀流","索隆");
properties.setProperty("程序员","小郑");
FileWriter writer = new FileWriter(path);
properties.store(writer,"测试");
writer.close();
运行结果:(properties存入的内容各自就是 xx=xx,key与value配对)

代码03:load
Properties properties = new Properties();
FileReader reader = new FileReader("D:\\Java_IDEA\\Java_Practice\\practice\\src\\com\\java\\properties_test\\测试.txt");
properties.load(reader);
Set<String> strs = properties.stringPropertyNames();
for(String str:strs){
System.out.println(str);
}
reader.close();
运行结果:

代码04:
Properties properties = new Properties();
FileInputStream stream = new FileInputStream("D:\\Java_IDEA\\Java_Practice\\practice\\src\\com\\java\\properties_test\\测试.txt");
properties.load(stream);
Set<String> strings = properties.stringPropertyNames();
for(String str:strings){
System.out.println(str);
}
stream.close();
运行结果:乱码

五.Properties在反射中的应用:
代码01:
//1.加载配置文件
//1.1创建Properties对象
Properties pro = new Properties();
//1.2j加载配置文件,转换为一个集合
//1.2.1获取class目录下的配置文件
ClassLoader loader = ReflectDemo4.class.getClassLoader();
InputStream in = loader.getResourceAsStream("pro.properties");
pro.load(in);
//2.获取配置文件中定义的数据
String className = pro.getProperty("className");
String methodName1 = pro.getProperty("methodName1");
String methodName2 = pro.getProperty("methodName2");
//3.加载该类j进内存
Class cls = Class.forName(className1);
//4.创建对象
Object obj = cls.newInstance();
//5.获取对象方法
Method sleep = cls.getMethod("sleep");
//6.执行
sleep.invoke(obj);
//7.有private修饰的,使用跟反射同理
Method eat = cls.getDeclaredMethod("eat");
eat.setAccessible(true);
eat.invoke(obj);
运行结果:

代码02:
InputStream in = PropertiesTest.class.getClassLoader().getResourceAsStream("pro.properties");
Properties properties = new Properties();
properties.load(in);
String classname2 = properties.getProperty("className2");
Class<?> clazz = Class.forName(classname2);
Constructor<?> constructor = clazz.getConstructor();
Cat obj = (Cat)constructor.newInstance();
obj.jiao();
本文详细介绍了Java中的Properties集合,它是HashTable的子类,用于存储键值对,常用于反射和框架中。Properties文件通常以.properties为后缀,可以通过store和load方法进行持久化读写。此外,文中通过示例展示了如何使用Properties进行文件写入和读取,并在反射中加载配置文件以执行相关操作。

644

被折叠的 条评论
为什么被折叠?



