java基础之对properties配置文件的读写操作
来源:互联网 发布:中国历史延续 知乎 编辑:程序博客网 时间:2024/04/30 14:35
1、test.properties文件处于与src并列的位置,即根目录
2、源码
/** * 1.clipse:test.properties文件处于与src并列的位置,即根目录 *//** * 2.Android studio:test.properties文件放于assets文件夹下 * 读入输出流方法:InputStream in =getAssets().open("test.txt"); * 获得AssetManger 对象, 调用其open 方法取得 对应的inputStream对象 */public class PropertiesTest { /** * 1.读取配置文件 * * @throws IOException */ @Test public void read() throws IOException { Properties properties = new Properties(); File file = new File("test.properties"); FileInputStream fis = new FileInputStream(file); try { // 读取配置文件 properties.load(fis); } catch (IOException e1) { e1.printStackTrace(); } Set<Object> keySet = properties.keySet(); for (Object object : keySet) { System.out.println(object.toString() + ":" + properties.getProperty(object.toString())); } } /** * 2.向配置文件写入内容 * * @throws IOException */ @Test public void write() throws IOException { Properties properties = new Properties(); String file = "test.properties"; OutputStream fos = new FileOutputStream(file); properties.setProperty("aa", "123"); properties.setProperty("bb", "456"); properties.setProperty("cc", "789"); properties.setProperty("name", "张三"); properties.store(fos, "保存文件"); fos.close(); } /** * 3.对配置文件的修改 * * @throws IOException */ @Test public void update() throws IOException { Properties properties = new Properties(); File file = new File("test.properties"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file); try { // 读取配置文件 properties.load(fis); } catch (IOException e1) { e1.printStackTrace(); } properties.setProperty("name", "李四"); properties.setProperty("age", "222"); properties.setProperty("age3", "222"); properties.store(fos, "更新配置文件"); fos.close(); fis.close(); } /** * 4.删除配置文件中的key * @throws IOException * */ @Test public void delete() throws IOException{ Properties properties = new Properties(); File file = new File("test.properties"); FileInputStream fis = new FileInputStream(file); try { // 读取配置文件 properties.load(fis); } catch (IOException e1) { e1.printStackTrace(); } //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了 Map<String, String> map = new HashMap<String, String>(); Set<Object> keySet = properties.keySet(); System.out.println(keySet.size()); for (Object object : keySet) { String key = (String) object; String value = (String) properties.get(key); System.out.println(key+"="+value); map.put(key, value); } //删除key map.remove("age3"); properties.remove("age3"); for (java.util.Map.Entry<String, String> entry: map.entrySet()) { properties.setProperty(entry.getKey(), entry.getValue()); } FileOutputStream fos = new FileOutputStream(file); properties.store(fos, "删除配置文件中的key:age3"); fos.close(); fis.close(); } }
0 0
- java基础之对properties配置文件的读写操作
- java对properties配置文件的读写操作
- java对properties配置文件的读写操作
- Java对properties文件的读写操作
- java对properties文件的读写操作
- Java对properties配置文件的操作
- 对Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- Java 读写Properties配置文件
- Java 读写Properties配置文件
- Java读写properties配置文件
- java读写properties配置文件
- Java 读写Properties配置文件
- Java读写Properties配置文件
- Java 读写Properties配置文件
- Java 读写Properties配置文件
- Java 读写Properties配置文件
- Java 读写Properties配置文件
- iOS开发记录<1>
- Android中SwipeRefreshLayout的使用
- 程明明-关于文献阅读和寻找新的科研题目
- hdu 1269 迷宫城堡
- 临界区,互斥量,信号量,事件的区别
- java基础之对properties配置文件的读写操作
- 黑马程序员——OC-Setter/Getter函数及@proterty和@synthesize
- 黑马程序员---集合框架(一)
- 数据结构例程——图的邻接矩阵存储结构及算法
- Java 8中的增强的Arrays
- 《剑指offer》——斐波那契数列
- 从系统角度看分布式系统的挑战
- 简单的二叉树创建及其基本的运算(C语言)
- OpenCV中IplImage图像格式与BYTE图像数据的转换