`
wxinpeng
  • 浏览: 581745 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

读写properties 文件

阅读更多
/**
 * 通用IO类。读取,写入properties文件
 */
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
public class PropertiesUtil {
	// 根据key读取value
	public static String readValue(File propsFile, String key) {
		Properties props = new Properties();
		InputStream inputStream;
		try {
			inputStream = new BufferedInputStream(new FileInputStream(propsFile));
			// 从输入流中读取属性列表(键和元素对)。
			props.load(inputStream);
			// 用指定的键在此属性列表中搜索属性。
			String value = props.getProperty(key);
			return value;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 读取properties的全部信息
	public static Map<String,String> readProperties(File file) {
		Properties props = new Properties();
		Map<String,String> map = new Hashtable<String,String>();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(file));
			props.load(in);
			// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String value = props.getProperty(key);
				map.put(key, value);
			}
			return map;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 写入properties信息
	public static void writeProperties(File file, String parameterName,
			String parameterValue) {
		Properties prop = new Properties();
		try {
			InputStream fis = new FileInputStream(file);
			// 从输入流中读取属性列表(键和元素对)
			prop.load(fis);
			// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
			// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
			OutputStream fos = new FileOutputStream(file);
			// 调用 Hashtable 的方法 put。
			prop.setProperty(parameterName, parameterValue);
			// 以适合使用 load 方法加载到 Properties 表中的格式,
			// 将此 Properties 表中的属性列表(键和元素对)写入输出流
			prop.store(fos, "Update '" + parameterName + "' value");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 批量写入properties信息
	public static void writeAllProperties(File file, Map<String,String> map) {
		Properties properties = new Properties();
		try {
			// 将文件读入流
			InputStream inputStream = new FileInputStream(file);
			// 加载文件
			properties.load(inputStream);
			// 准备写入流
			OutputStream outputStream = new FileOutputStream(file);
			// 将信息放入Properites
			properties.putAll(map);
			// 保存此Properites
			properties.store(outputStream, "Update '" + map);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

分享到:
评论
1 楼 xz_dukaikai 2011-06-15  
试了一下,在使用key value写入的时候只能写入一个key value,,如我要在不同的方法里写入好多次的话,就不行。。。 属性文件只能写入一个

相关推荐

Global site tag (gtag.js) - Google Analytics