java学习(properties)
2016-12-10 22:45
375 查看
public class LearnProperties { public static void main(String[] args) throws IOException{ Properties prop = new Properties(); FileInputStream fi = new FileInputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties"); //FileOutputStream fo = new FileOutputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties"); prop.load(fi); System.out.println("name: " + prop.getProperty("name")); System.out.println("age: " + prop.getProperty("age", "18")); //若配置文件中为有salary这个key,则返回默认值18w System.out.println("salary: " + prop.getProperty("salary", "18w")); FileOutputStream fo = new FileOutputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties"); prop.setProperty("salary", "5 yuan"); //保存回配置文件中 prop.store(fo,""); Properties prop2 = new Properties(); prop2.load(fi); System.out.println("salary: " + prop2.getProperty("salary", "18w")); fo.close(); } }
FileOutputStream不要紧接着FileInputStream定义,这样会覆盖掉FileInputStream,导致加载到prop中的内容是空
Properties可以方便的对配置文件进行读写操作
配置文件格式为key=value形式,如下为一个配置文件内容
age=22 name=zxye
以等号划分。
Properties类实现了HashTable接口,因此,在Proterties中的key-value是以Hash形式存储起来。
最后输出结果
name: zxye age: 22 salary: 18w salary: 5 yuan
参考: http://blog.csdn.net/qinpeng100423/article/details/9125857
相关文章推荐
- JAVA学习提高之----使用J2SE API读properties文件的六种方法
- java.util.Properties源码学习
- Java-马士兵设计模式学习笔记-观察者模式-读取properties文件,动态增加观察者
- Java学习-021-Properties 获取配置项对应的值
- Java学习基础组件---Properties文件的读取
- Java学习笔记(八)Dictionary,HashTable,Properties
- JavaIo学习Properties
- JAVA学习笔记44——其他容器简介2:HashTable+Properties
- 重踏学习Java路上_Day22(数据操作流,内存操作流,打印流,标准输入输出流,随机访问流,合并流,序列化流,Properties,NIO)
- Java学习笔记——JDBC读取properties属性文件
- Java学习第20天:IO流中的File对象和一些只是(如Properties)
- 关于Java配置文件properties的学习
- JAVA学习提高之----Java的多种方式读写Properties文件
- Java学习笔记(二)——Java操作properties文件
- 13. Java类集 Part 2(Map接口、SortedMap接口、集合工具类Collection、Stack类、属性类Properties) ----- 学习笔记
- JAVA学习课第五十三届 — IO流程(七)File打靶 & Properties设置