java操作properties文件

package com.ccdevote.www.common.PropertiesDeal;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Properties 類存在於包 Java.util 中,該類繼承自 Hashtable ,它提供了幾個主要的方法: 1. load
 * (nputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對)。 2. getProperty(String key),
 * 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value 。 3. setProperty ( String
 * key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。
 * 通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty (
 * String key)
 * 
 * 來搜索。 4. store(OutputStream out,String comments) ,以適合使用 load 方法加載到 Properties
 * 表中的格式,將此 Properties 表中的屬性列
 * 
 * 表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。 5. clear() ,清除所有裝載的 鍵 -
 * 值對。該方法在基類中提供。 有了以上幾個方法我們就可以對 .properties 文件進行操作了! 總結:
 * java的properties文件需要放到classpath下面,這樣程序才能讀取到, 有關classpath實際上就是java類或者庫的存放路徑:
 * 在java工程中,properties放到class文件一塊; 在web應用中,最簡單的方法是放到web應用的WEB-INF\classes 目錄下即可,
 * 也可以放在其他文件夾下面,這時候需要在設置classpath環境變量的時候,將這個文件夾路徑加到classpath變量中,這樣也也可以讀取到
 * 
 * 。 在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變量,WEB-INF\classes其實也是,
 * java工程的
 * 
 * class文件目錄也是。
 */

public class PropertiesTools {
	/*
	 * 讀取properties的全部信息
	 */
	

	public static void readProperties(String fileNamePath) throws IOException {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(fileNamePath));
			// 如果將in改爲下面的方法,必須要將.Properties文件和此class類文件放在同一個包中
			// in = propertiesTools.class.getResourceAsStream(fileNamePath);
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String Property = props.getProperty(key);
				System.out.println(key + "=" + Property);
			}
		} catch (Exception e) {
			if (null != in)
				in.close();
			e.printStackTrace();
		} finally {
			if (null != in)
				in.close();
		}
	}

	/*
	 * 根據key讀取value
	 */
	public static String getValue(String fileNamePath, String key)
			throws IOException {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = new FileInputStream(fileNamePath);
			// 如果將in改爲下面的方法,必須要將.Properties文件和此class類文件放在同一個包中
			// in = propertiesTools.class.getResourceAsStream(fileNamePath);
			props.load(in);
			String value = props.getProperty(key);
			// 有亂碼時要進行重新編碼
			// new String(props.getProperty("name").getBytes("ISO-8859-1"),
			// "GBK");
			return value;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (null != in)
				in.close();
		}

	}

	/*
	 * 寫入properties信息
	 */
	public static void setProperties(String fileNamePath, String parameterName,
			String parameterValue) throws IOException {
		Properties prop = new Properties();
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(fileNamePath);
			// 如果將in改爲下面的方法,必須要將.Properties文件和此class類文件放在同一個包中
			// in = propertiesTools.class.getResourceAsStream(fileNamePath);
			prop.load(in);
			out = new FileOutputStream(fileNamePath);
			prop.setProperty(parameterName, parameterValue);
			prop.store(out, parameterName);// 保存
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != in)
				in.close();
			if (null != out)
				out.close();
		}
	}

	public static void main(String[] args) throws IOException {
		// 方法一:
		// String fileNamePath = "info.properties";
		// 上面不變,用類中註釋掉的in =
		// TestProperties.class.getResourceAsStream(fileNamePath);方法,必須要將.Properties文件和此
		
		// class類文件放在同一個包中
	
		// 方法二:
		String fileNamePath = "config//info.properties";
		// 這個方法需要在項目的要目錄下建立config文件夾,然後把properties文件放在config下

		// 取值
		System.out.println("取username的值:" + getValue(fileNamePath, "username"));
		System.out.println("取age的值:" + getValue(fileNamePath, "age"));
		// 寫值
		setProperties(fileNamePath, "age", "21");
		// 取此文件所有配置信息
		readProperties(fileNamePath);
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章