package cn.properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;
/**
* 今天呢,整理以下项目中关于properties类的使用;
*
* properties类:
* 能够读取或存储键值对的数据信息;(继承自Hashtable)
*
* 接下来走来两个简单的Demo;
*
* 还是和之前一样,首先创建一个自己的测试类;
* @author Administrator
*
*/
public class PropertiesTest {
/**
* Java主函数|入口!
* @param args
* @throws FileNotFoundException
* @throws IOException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// 实例化properties对象;
Properties pro = new Properties();
// 装载数据!
pro.setProperty("username", "admin");
pro.setProperty("password", "123456");
pro.setProperty("url", "www.baidu.com");
// 获取指定的键值;
pro.get("url");
/**
* 加载指定文件;
* 键值对格式:
* key:value;
* 或者
* key=value;
*/
// pro.load(new FileInputStream("D:\\2020\\d.txt"));
pro.load(new FileInputStream("D:\\2020\\d.properties"));
/**
* 数据写入;
* store(OutputStream out, String comments);
* OutputStream out: 指定的流数据对象;
* String comments: 写入流对象数据后添加的注释;
*/
pro.store(new FileOutputStream("D:\\2020\\d.properties",true), "annotation!");
/**
* 获取properties对象的所有key值;
* 通过key值进行数据遍历;
*/
Set<String> stringPropertyNames = pro.stringPropertyNames();
for(String strPro : stringPropertyNames){
System.out.println(pro.get(strPro));
}
}
}