Properties文件的读写 : Properties操作示例
编程技术  /  houtizong 发布于 3年前   74
package properties;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;/** * Java配置文件Properties类的的读、写及更新操作 */public class SetSystemProperty {// 属性文件的路径static String profilepath = "mail.properties";/** * 采用静态方法 */private static Properties props = new Properties();static {try {props.load(new FileInputStream(profilepath));} catch (FileNotFoundException e) {e.printStackTrace();System.exit(-1);} catch (IOException e) {System.exit(-1);}}/** * 读取属性文件中相应键的值 * * @param key * 主键 * @return String */public static String getKeyValue(String key) {return props.getProperty(key);}/** * 根据主键key读取主键的值value * * @param filePath * 属性文件路径 </p> * 在工程中取得相对路径的方法 :this.getClass().getResource("工程路径").getPath(); * @param key * 键名 */public static String readValue(String filePath, String key) {Properties props = new Properties();try {InputStream in = new BufferedInputStream(new FileInputStream(filePath));props.load(in);String value = props.getProperty(key);System.out.println(key + "键的值是:" + value);return value;} catch (Exception e) {e.printStackTrace();return null;}}/** * 更新(或插入)一对properties信息(主键及其键值) * <br>如果该主键已经存在,更新该主键的值;</br> * <br>如果该主键不存在,则插件一对键值。</br> * * @param keyname * 键名 * @param keyvalue * 键值 */public static void writeProperties(String keyname, String keyvalue) {try {OutputStream fos = new FileOutputStream(profilepath);props.setProperty(keyname, keyvalue);// 以适合使用 load 方法加载到 Properties 表中的格式,// 将此 Properties 表中的属性列表(键和元素对)写入输出流props.store(fos, "Update '" + keyname + "' value");} catch (IOException e) {System.err.println("属性文件更新错误");}}/** * 更新properties文件的键值对 * <br>如果该主键已经存在,更新该主键的值;</br> * <br>如果该主键不存在,则插件一对键值。</br> * * @param keyname * 键名 * @param keyvalue * 键值 */public void updateProperties(String keyname, String keyvalue) {try {props.load(new FileInputStream(profilepath));OutputStream fos = new FileOutputStream(profilepath);props.setProperty(keyname, keyvalue);// 以适合使用 load 方法加载到 Properties 表中的格式,// 将此 Properties 表中的属性列表(键和元素对)写入输出流props.store(fos, "Update '" + keyname + "' value");} catch (IOException e) {System.err.println("属性文件更新错误");}}/** * 读取properties的全部信息 * * @param filePath * 文件的路径 */public static void readProperties(String filePath) {Properties props = new Properties();InputStream in = null;try {in = new BufferedInputStream(new FileInputStream(filePath));props.load(in);Enumeration en = props.propertyNames();while (en.hasMoreElements()) {String key = (String) en.nextElement();String property = props.getProperty(key);System.out.println(key + " : " + property);}} catch (Exception e) {e.printStackTrace();} finally {try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}// 测试代码public static void main(String[] args) {readValue("mail.properties", "MAIL_SERVER_PASSWORD");writeProperties("MAIL_SERVER_INCOMING", "[email protected]");System.out.println("操作完成");}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接