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

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);
        }    }}

 

转载于:https://my.oschina.net/u/3563297/blog/1557142

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值