您的位置:首页 > 运维架构

输出配置文件config.properties内容实例

2017-10-27 00:00 393 查看
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

public class PropertyTest {
public static void main(String[] args) {
Properties prop = new Properties();

try {// 读取属性文件config.properties--用输入流类把配置文件读取出来
//BufferedInputStream(InputStream in)  创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用,想当于创建一个缓冲区。
//InputStream in = new BufferedInputStream(new FileInputStream(
//"D:\\myselenium\\config.properties"));
//InputStream in = ClassLoader.class.getResourceAsStream("/properties/config.properties");//读取SRC下的配置文件
//ClassLoader这个只能针对配置文件放于另一个文件夹下才可用(也就是与这个包名同级,另外配置文件路径以实际所放路径而定,如果放在文件夹下,则直接写配置文件名即可),如果配置文件与类都是同级,则不能使用ClassLoader,否则报错
InputStream in=new FileInputStream("D:\\myselenium\\config.properties");
prop.setProperty("后管UAT", "http://soasadmin-stg.paic.com.cn/admin/admin/login.html");
prop.load(in); // /加载属性列表
Iterator<String> it = prop.stringPropertyNames().iterator();//stringPropertyNames方法返回一个Set键集,iterator()返回一个迭代元素的迭代器
while (it.hasNext()) {//如果仍有元素可以迭代,则返回 true
String key = it.next();//返回迭代的下一个元素
System.out.println(key + ":" + prop.getProperty(key));//获取指定键的属性值
}
in.close();

// /保存属性到b.properties文件
// FileOutputStream oFile = new FileOutputStream("config.properties",    true);//true表示追加打开
//prop.setProperty("phone", "10086");
//prop.store(oFile, "The New properties file");
// oFile.close();

} catch (Exception e) {
System.out.println(e);
}    }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐