package Properties;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class demo1 {
public static void main(String[] args) {
/*
Properties作为Map集合的操作
*/
//1.创建集合的对象
Properties p=new Properties();
//2.添加数据
//细节:虽然我们可以往Properties当中添加任意的数据类型,但是一般只会往里面添加字符串类型的数据
p.put("001","aaa");
p.put("002","bbb");
p.put("003","ccc");
//3.遍历properties集合
Set<Map.Entry<Object, Object>> entries = p.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
package Properties;
import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class demo2 {
public static void main(String[] args) throws IOException {
/*
Properties跟IO流结合的操作
*/
//1.创建集合的对象
Properties p=new Properties();
//2.添加数据
//细节:虽然我们可以往Properties当中添加任意的数据类型,但是一般只会往里面添加字符串类型的数据
p.put("001","aaa");
p.put("002","bbb");
p.put("003","ccc");
//3.把集合里的数据写到本地文件当中
//方法1
/* BufferedWriter br=new BufferedWriter( new FileWriter("test.txt"));
Set<Map.Entry<Object, Object>> entries = p.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
br.write(key+"="+value);
br.newLine();
}
br.close();*/
//方法2
//参数一:输出流
//参数2 :注释
FileOutputStream fos=new FileOutputStream("test.txt");
p.store(fos,"test");
fos.close();
}
}
package Properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class demo3 {
public static void main(String[] args) throws IOException {
//1.创建集合
Properties p = new Properties();
//2.读取本地Properties文件里面的数据
FileInputStream fis=new FileInputStream("test.txt");
//把文件里面的数据加载到集合中
p.load(fis);
fis.close();
//3.打印集合
System.out.println(p);
}
}