Properties讀取load(),操做,存儲store()配置文件簡介

http://blog.csdn.net/wjw_java/article/details/7950765 java

  1.   
  2. import java.io.BufferedReader;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.util.Iterator;  
  10. import java.util.Properties;  
  11. import java.util.Set;  
  12.   
  13. /** 
  14.  * Properties是hashtable的子類,也就是說它具有map集合的特色,並且它裏面存儲的鍵值對都是字符串 
  15.  * Properties是集合和IO相結合的集合容器 
  16.  * 該對象的特色能夠用於鍵值對形式的配置開發 
  17.  * 那麼在加載數據時,須要數據有固定格式,鍵=值 
  18.  *  
  19.  * p.load(new FileReader("路徑"));//加載配置文件 
  20.  * p.stote(new FileWriter("路徑"),"");//把修改的信息存儲在配置文件中 
  21.  *  
  22.  *  
  23.  * @author wjw 
  24.  */  
  25. public class Properties_class {  
  26.     public static void main(String[] args) {  
  27. //      setGetProperties();   
  28.         //duQu();   
  29.         load_store();  
  30. //      load_store1();   
  31.     }  
  32.     /* 
  33.      * Properties類中的load()方法,讀取文件到Properties對象中 
  34.      * Properties類中的store()方法,將Properties對象中數據存儲到文件中 
  35.      *  
  36.      * 提醒load,store分別都有字符流,字節流的方法的重載. 
  37.      */  
  38.     public static void load_store(){  
  39.         Properties p=new Properties();  
  40.         FileReader fr=null;  
  41.         FileWriter fw=null;  
  42.           
  43.         try {  
  44.             fr=new FileReader("g:/java/text.txt");  
  45.             p.load(fr);//load()方法可經過字符流直接加載文件   
  46.             p.setProperty("b","8888888888");  
  47.             fw=new FileWriter("g:/java/text.txt");  
  48.             p.store(fw,"jack");//將Properties中的信息存儲到文件中   
  49.             System.out.println(p);  
  50.             p.list(System.out);//將信息打印到控制檯   
  51.         } catch (FileNotFoundException e) {  
  52.             e.printStackTrace();  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         }finally{  
  56.             try {  
  57.                 if(fr!=null){  
  58.                     fr.close();  
  59.                 }  
  60.             } catch (Exception e2) {  
  61.             }  
  62.             try {  
  63.                 if(fw!=null){  
  64.                     fw.close();  
  65.                 }  
  66.             } catch (Exception e2) {  
  67.             }  
  68.         }  
  69.     }  
  70.     public static void load_store1(){  
  71.         Properties p=new Properties();  
  72.         FileInputStream fis=null;  
  73.         FileOutputStream fos=null;  
  74.           
  75.         try {  
  76.             fis=new FileInputStream("g:/java/text.txt");  
  77.             p.load(fis);//load()方法可經過字符流直接加載文件   
  78.               
  79.             //p.remove("lisi");   
  80.             p.setProperty("a","11111");  
  81.             fos=new FileOutputStream("g:/java/text.txt");  
  82.             p.store(fos,"jack");//將Properties中的信息存儲到文件中   
  83.               
  84.             System.out.println(p);  
  85.             p.list(System.out);//將信息打印到控制檯   
  86.         } catch (FileNotFoundException e) {  
  87.             e.printStackTrace();  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }finally{  
  91.             try {  
  92.                 if(fis!=null){  
  93.                     fis.close();  
  94.                 }  
  95.             } catch (Exception e2) {  
  96.             }  
  97.             try {  
  98.                 if(fos!=null){  
  99.                     fos.close();  
  100.                 }  
  101.             } catch (Exception e2) {  
  102.             }  
  103.         }  
  104.     }  
  105.       
  106.     /*  
  107.      * 遍歷Properties類中的鍵值對 
  108.      */  
  109.     public static void bianLi(Properties p){  
  110.         Set<String>set=p.stringPropertyNames();  
  111.         for(String str:set){  
  112.             System.out.println("鍵:"+str+"----值:"+p.getProperty(str));  
  113.         }  
  114.     }  
  115.     /* 
  116.      * 讀取配置文件中的數據放到Properties集合中,這實際上是Properties類中load()方法的原理 
  117.      */  
  118.     public static void duQu(){  
  119.         BufferedReader br=null;  
  120.         Properties p=new Properties();  
  121.         try {  
  122.             br=new BufferedReader(new FileReader("g:/java/text.txt"));  
  123.             String str="";  
  124.             String[] split=new String[2];  
  125.             while(null!=(str=br.readLine())){  
  126.                 split=str.split("=");//解析數據   
  127.                 p.setProperty(split[0],split[1]);//將數據存儲到Properties中   
  128.             }  
  129.             bianLi(p);//調用遍歷Properties類的方法   
  130.         } catch (IOException e) {  
  131.             e.printStackTrace();  
  132.         }finally{  
  133.             try {  
  134.                 if(null!=br){  
  135.                     br.close();  
  136.                 }  
  137.             } catch (Exception e2) {  
  138.                  e2.printStackTrace();  
  139.             }  
  140.         }  
  141.     }  
  142.       
  143.     /* 
  144.      * Properties類中的getProtertity(),setProtertity()方法 
  145.      */  
  146.     public static void setGetProperties(){  
  147.         Properties p=new Properties();  
  148.         p.setProperty("zhangsan""33");//設定Properties類中存放的鍵值對   
  149.         p.setProperty("lisi""22");  
  150.         p.setProperty("wangwu""3563");  
  151.         p.setProperty("zhaoliu""3663");  
  152.           
  153.         p.setProperty("lisi","9999");//修改lisi的值,原來的值會被覆蓋掉   
  154.         System.out.println(p);//直接打印集合可獲取集合信息   
  155.           
  156.         String zhi=p.getProperty("lisi");//根據鍵獲取值   
  157.         System.out.println(zhi);  
  158.         //用原始方法遍歷Properties集合   
  159.         Set<Object> set=p.keySet();  
  160.         Iterator<Object> iter=set.iterator();  
  161.         while(iter.hasNext()){  
  162.             String str=(String)iter.next();  
  163.             System.out.println(str+":"+p.getProperty(str));  
  164.         }  
  165.         System.out.println("---------------------------------------------");  
  166.         //用Properties類中定義的方法遍歷集合:   
  167.         Set<String>set_str=p.stringPropertyNames();  
  168.         for(String str_a:set_str){  
  169.             System.out.println(str_a+":"+p.getProperty(str_a));  
  170.         }  
  171.     }  
  172. }  
package com.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * Properties是hashtable的子類,也就是說它具有map集合的特色,並且它裏面存儲的鍵值對都是字符串
 * Properties是集合和IO相結合的集合容器
 * 該對象的特色能夠用於鍵值對形式的配置開發
 * 那麼在加載數據時,須要數據有固定格式,鍵=值
 * 
 * p.load(new FileReader("路徑"));//加載配置文件
 * p.stote(new FileWriter("路徑"),"");//把修改的信息存儲在配置文件中
 * 
 * 
 * @author wjw
 */
public class Properties_class {
	public static void main(String[] args) {
//		setGetProperties();
		//duQu();
		load_store();
//		load_store1();
	}
	/*
	 * Properties類中的load()方法,讀取文件到Properties對象中
	 * Properties類中的store()方法,將Properties對象中數據存儲到文件中
	 * 
	 * 提醒load,store分別都有字符流,字節流的方法的重載.
	 */
	public static void load_store(){
		Properties p=new Properties();
		FileReader fr=null;
		FileWriter fw=null;
		
		try {
			fr=new FileReader("g:/java/text.txt");
			p.load(fr);//load()方法可經過字符流直接加載文件
			p.setProperty("b","8888888888");
			fw=new FileWriter("g:/java/text.txt");
			p.store(fw,"jack");//將Properties中的信息存儲到文件中
			System.out.println(p);
			p.list(System.out);//將信息打印到控制檯
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fr!=null){
					fr.close();
				}
			} catch (Exception e2) {
			}
			try {
				if(fw!=null){
					fw.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	public static void load_store1(){
		Properties p=new Properties();
		FileInputStream fis=null;
		FileOutputStream fos=null;
		
		try {
			fis=new FileInputStream("g:/java/text.txt");
			p.load(fis);//load()方法可經過字符流直接加載文件
			
			//p.remove("lisi");
			p.setProperty("a","11111");
			fos=new FileOutputStream("g:/java/text.txt");
			p.store(fos,"jack");//將Properties中的信息存儲到文件中
			
			System.out.println(p);
			p.list(System.out);//將信息打印到控制檯
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (Exception e2) {
			}
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	
	/* 
	 * 遍歷Properties類中的鍵值對
	 */
	public static void bianLi(Properties p){
		Set<String>set=p.stringPropertyNames();
		for(String str:set){
			System.out.println("鍵:"+str+"----值:"+p.getProperty(str));
		}
	}
	/*
	 * 讀取配置文件中的數據放到Properties集合中,這實際上是Properties類中load()方法的原理
	 */
	public static void duQu(){
		BufferedReader br=null;
		Properties p=new Properties();
		try {
			br=new BufferedReader(new FileReader("g:/java/text.txt"));
			String str="";
			String[] split=new String[2];
			while(null!=(str=br.readLine())){
				split=str.split("=");//解析數據
				p.setProperty(split[0],split[1]);//將數據存儲到Properties中
			}
			bianLi(p);//調用遍歷Properties類的方法
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(null!=br){
					br.close();
				}
			} catch (Exception e2) {
				 e2.printStackTrace();
			}
		}
	}
	
	/*
	 * Properties類中的getProtertity(),setProtertity()方法
	 */
	public static void setGetProperties(){
		Properties p=new Properties();
		p.setProperty("zhangsan", "33");//設定Properties類中存放的鍵值對
		p.setProperty("lisi", "22");
		p.setProperty("wangwu", "3563");
		p.setProperty("zhaoliu", "3663");
		
		p.setProperty("lisi","9999");//修改lisi的值,原來的值會被覆蓋掉
		System.out.println(p);//直接打印集合可獲取集合信息
		
		String zhi=p.getProperty("lisi");//根據鍵獲取值
		System.out.println(zhi);
		//用原始方法遍歷Properties集合
		Set<Object> set=p.keySet();
		Iterator<Object> iter=set.iterator();
		while(iter.hasNext()){
			String str=(String)iter.next();
			System.out.println(str+":"+p.getProperty(str));
		}
		System.out.println("---------------------------------------------");
		//用Properties類中定義的方法遍歷集合:
		Set<String>set_str=p.stringPropertyNames();
		for(String str_a:set_str){
			System.out.println(str_a+":"+p.getProperty(str_a));
		}
	}
}
 
相關文章
相關標籤/搜索