Java-I/O框架14:Properties集合及使用
1.Properties集合
特性:
- 存储属性名和属性值;
- 属性名和属性值都是字符串类型;
- 没有泛型;
- 和流有关
2.Properties集合使用
public class PropertiesDemo01 {
public static void main(String[] args) throws Exception {
//1创建集合
Properties properties = new Properties();
//2添加元素
properties.setProperty("username","root");
properties.setProperty("age","20");
//3打印集合元素
System.out.println(properties.toString());
//4遍历集合
//4.1------使用keySet()遍历------(自己补齐)
//4.2------使用entrySet()遍历------(自己补齐)
//4.3------使用stringPropertyNames()遍历------
Set<String> propertyNames = properties.stringPropertyNames();
for (String propertyName : propertyNames) {
System.out.println(propertyName + "======" + properties.getProperty(propertyName));
}
//5和流有关的方法
//5.1--------list()方法-------
PrintWriter printWriter = new PrintWriter("C:\\kkk.txt");
properties.list(printWriter);
printWriter.close();
//5.2--------store()方法,实现保存-------
FileOutputStream fos = new FileOutputStream("C:\\kkk.properties");
properties.store(fos,"Mr zhang");
fos.close();
//5.3--------load()方法,实现数据加载-------
Properties properties1 = new Properties();
FileInputStream fis = new FileInputStream("C:\\kkk.properties");
properties1.load(fis);
fis.close();
System.out.println("--------------");
System.out.println(properties1.toString());
}
}
原文地址:https://blog.csdn.net/zhangjinajian759/article/details/143651272
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!