【Java编程】写入、读取、遍历Properties文件

本文介绍如何使用Java操作Properties文件,包括向文件写入键值对、读取键值对及遍历所有键值对的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/andie_guo/article/details/25425403
【Java编程】写入、读取、遍历Properties文件

Java 开发中通常我们会存储配置参数信息到属性文件,这样的属性文件可以是拥有键值对的属性文件,也可以是XML文件,关于XML文件的操作,请参考博文 【Java编程】DOM XML Parser 解析、遍历、创建XML 。在该篇博文中,我将展示如何向属性文件写入键值对,如何读取属性文件中的键值对,如何遍历属性文件。

1、向属性文件中写入键值对


特别注意:

Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames()方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store()方法持久化键值对到属性文件中,这里的store方法类似于Android SharedPreferences的commit()方法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.andieguo.propertiesdemo;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.Date;  
  9. import java.util.Enumeration;  
  10. import java.util.Properties;  
  11.   
  12. import junit.framework.TestCase;  
  13.   
  14. public class PropertiesTester extends TestCase {  
  15.   
  16.     public void writeProperties() {  
  17.         Properties properties = new Properties();  
  18.         OutputStream output = null;  
  19.         try {  
  20.             output = new FileOutputStream("config.properties");  
  21.             properties.setProperty("url""jdbc:mysql://localhost:3306/");  
  22.             properties.setProperty("username""root");  
  23.             properties.setProperty("password""root");  
  24.             properties.setProperty("database""bbs");//保存键值对到内存  
  25.             properties.store(output, "andieguo modify" + new Date().toString());// 保存键值对到文件中  
  26.         } catch (IOException io) {  
  27.             io.printStackTrace();  
  28.         } finally {  
  29.             if (output != null) {  
  30.                 try {  
  31.                     output.close();  
  32.                 } catch (IOException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38. }  
执行单元测试后,属性文件内容如下:


2、读取属性文件中的键值对


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class PropertiesTester extends TestCase {  
  2.   
  3.     public void loadProperties() {  
  4.         Properties properties = new Properties();  
  5.         InputStream input = null;  
  6.         try {  
  7.             input = new FileInputStream("config.properties");//加载Java项目根路径下的配置文件  
  8.             properties.load(input);// 加载属性文件  
  9.             System.out.println("url:" + properties.getProperty("url"));  
  10.             System.out.println("username:" + properties.getProperty("username"));  
  11.             System.out.println("password:" + properties.getProperty("password"));  
  12.             System.out.println("database:" + properties.getProperty("database"));  
  13.         } catch (IOException io) {  
  14.         } finally {  
  15.             if (input != null) {  
  16.                 try {  
  17.                     input.close();  
  18.                 } catch (IOException e) {  
  19.                     e.printStackTrace();  
  20.                 }  
  21.             }  
  22.         }  
  23.     }  
  24. }  

执行单元测试方法,console输出的output如下:


3、遍历属性文件中的键值对

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.andieguo.propertiesdemo;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Enumeration;  
  6. import java.util.Map.Entry;  
  7. import java.util.Properties;  
  8. import java.util.Set;  
  9.   
  10. import junit.framework.TestCase;  
  11.   
  12. public class PropertiesTester extends TestCase {  
  13.   
  14.     public void printAll() {  
  15.         Properties prop = new Properties();  
  16.         InputStream input = null;  
  17.         try {  
  18.             String filename = "config.properties";  
  19.             input = getClass().getClassLoader().getResourceAsStream(filename);  
  20.             if (input == null) {  
  21.                 System.out.println("Sorry, unable to find " + filename);  
  22.                 return;  
  23.             }  
  24.             prop.load(input);  
  25.             //方法一:  
  26.             Set<Object> keys = prop.keySet();//返回属性key的集合  
  27.             for(Object key:keys){  
  28.                 System.out.println("key:"+key.toString()+",value:"+prop.get(key));  
  29.             }  
  30.             //方法二:  
  31.             Set<Entry<Object, Object>> entrys = prop.entrySet();//返回的属性键值对实体  
  32.             for(Entry<Object, Object> entry:entrys){  
  33.                 System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());  
  34.             }  
  35.             //方法三:  
  36.             Enumeration<?> e = prop.propertyNames();  
  37.             while (e.hasMoreElements()) {  
  38.                 String key = (String) e.nextElement();  
  39.                 String value = prop.getProperty(key);  
  40.                 System.out.println("Key:" + key + ",Value:" + value);  
  41.             }  
  42.         } catch (IOException ex) {  
  43.             ex.printStackTrace();  
  44.         } finally {  
  45.             if (input != null) {  
  46.                 try {  
  47.                     input.close();  
  48.                 } catch (IOException e) {  
  49.                     e.printStackTrace();  
  50.                 }  
  51.             }  
  52.         }  
  53.     }  
  54.       
  55. }  

4、其他方法

public void list(PrintStream out)

将属性列表输出到指定的输出流。此方法对调试很有用。

public void storeToXML(OutputStream os,Stringcomment) throws IOException

发出一个表示此表中包含的所有属性的 XML 文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值