Java I/O---Properties类(持久化键值对)

1.Properties类简介

       Properties类(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

#person information
#Sat Dec 30 15:00:22 CST 2017
age=18
name=zhangsan

 

 

      

          Properties的父类为 HashTable类。

Propertise类常用方法:

(1)setProperty ( String key, String value) : 调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

(2)getProperty ( String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

(3)load ( InputStream inStream):从输入流中读取属性列表(键和元素对)。

(4)store ( OutputStream out, String comments):将Properties 表中的属性列表(键和元素对)写入输出流。comments为注释参数。该方法将键 - 值对写入到指定的文件中去。

(5)list(System.out):打印配置文件信息到控制台。

(6)clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

 

 2.properties方法演示

  1 public class PropertiesDemo {
  2 
  3 	/**
  4 	 * Properties 常用到配置文件参数
  5 	 * @param args
  6 	 */
  7 	public static void main(String[] args) {
  8 		// TODO Auto-generated method stub
  9 
 10 //		methodDemo();	
 11 //		methodDemo2();
 12 		methodDemo3();
 13 	}
 14 
 15 	private static void methodDemo3() {
 16 		// TODO Auto-generated method stub
 17 		Properties prop = new Properties();
 18 
 19 		//创建输ru流对象
 20 		FileInputStream inStream = null;
 21 		try {
 22 			inStream = new FileInputStream("H:\\workspace\\Testfile\\info.properties");
 23 			prop.load(inStream);//加载数据
 24 
 25 			prop.setProperty("name", "zhangsan");//修改参数
 26 
 27 			prop.list(System.out);//打印
 28 
 29 
 30 		} catch (IOException e) {
 31 			// TODO Auto-generated catch block
 32 			e.printStackTrace();
 33 		} finally{
 34 			if(inStream != null){
 35 				try {
 36 					inStream.close();
 37 				} catch (IOException e) {
 38 					// TODO Auto-generated catch block
 39 					throw new RuntimeException();
 40 				}
 41 			}
 42 		}
 43 
 44 
 45 	}
 46 
 47 	/**
 48 	 * 演示properties持久化
 49 	 */
 50 	private static void methodDemo2() {
 51 		// TODO Auto-generated method stub
 52 		Properties prop = new Properties();
 53 
 54 		prop.setProperty("name", "zhangsan");
 55 		prop.setProperty("age", "18");
 56 
 57 		//创建输出流对象
 58 		FileOutputStream fos = null;
 59 		try {
 60 			fos = new FileOutputStream("H:\\workspace\\Testfile\\info.properties");
 61 			prop.store(fos, "person information");//第二个参数为注释信息
 62 
 63 
 64 
 65 		} catch (IOException e) {
 66 			// TODO Auto-generated catch block
 67 			e.printStackTrace();
 68 		} finally{
 69 			if(fos != null){
 70 				try {
 71 					fos.close();
 72 				} catch (IOException e) {
 73 					// TODO Auto-generated catch block
 74 					throw new RuntimeException();
 75 				}
 76 			}
 77 		}
 78 
 79 	}
 80 
 81 	/**
 82 	 * 演示setProperty()和prop.list(System.out)
 83 	 */
 84 	private static void methodDemo() {
 85 		// TODO Auto-generated method stub
 86 		Properties prop = new Properties();
 87 
 88 		prop.setProperty("name", "zhangsan");
 89 		prop.setProperty("age", "18");
 90 
 91 		prop.list(System.out);//直接使用list方法打印输出
 92 
 93 	/*
 94 		Set<String> set = prop.stringPropertyNames();
 95 
 96 		for(String key : set){
 97 			String value = prop.getProperty(key);
 98 			System.out.println(key+":"+value);
 99 		}*/
100 
101 	}
102 
103 }

 

2017-12-30 内容来自创智播客课程

Java中Properties类的操作 - bakari - 博客园

转载于:https://www.cnblogs.com/zhuweiheng/p/8150149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值