关于properties文件读取 傷城~ 2022-08-21 14:55 116阅读 0赞 # \-----读取----- # ###### load(InputStream inStream) 这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。 ###### Properties prop = new Properties(); //读取属性文件test.properties(BufferedInputStream 输入流) InputStream in = new BufferedInputStream (new FileInputStream("test.properties")); //加载属性列表 prop.load(in); //遍历所有key(属性名) Iterator<String> it=prop.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); //获取value(属性值) System.out.println(key+":"+prop.getProperty(key)); } //关闭流 in.close(); # **-----写入-----** # ###### store(OutputStream out, String comments) 这个方法将**Properties类对象的属性列表保存到输出流中**。 ###### Properties prop = new Properties(); //保存属性到test2.properties文件 FileOutputStream out = new FileOutputStream("test2.properties", true);//true表示追加打开 //设置value(属性值) prop.setProperty("phone", "10086"); //如果"The New properties file"不为空,保存后的属性文件第一行会是#The New properties file表示注释信息;如果为空则没有注释信息。 prop.store(out, "The New properties file"); //关闭流 out.close();
还没有评论,来说两句吧...