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