properties文件的存取与Map键值对排序

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;

public class Test {

	public static void main(String[] args) {
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("aa", "1");
		map1.put("bb", "3");
		map1.put("cc", "2");
		writeProperties("info.properties", map1);
		
		Map map = getBundleInfoMap("Info");
		
		Map.Entry<String, Integer>[] entry = getSortedHashtableByValue(map);
		
		for (int i = 0; i < entry.length; i++) {
			System.out.println(entry[i]);
		}
		
	}
	
	public static Map<String, Integer> getBundleInfoMap(String bundleName) {
		ResourceBundle rb = ResourceBundle.getBundle(bundleName);
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		Enumeration<String> en = rb.getKeys();
		
		while(en.hasMoreElements()) {
			String key = en.nextElement();
			map.put(key, Integer.valueOf(rb.getString(key)));
		}
		return map;
	}

	/**
	 * @param map
	 * @return the entries of the map after being sorted
	 */
	@SuppressWarnings("unchecked")
	public static Map.Entry[] getSortedHashtableByValue(Map<String, Integer> map) {
		Set set = map.entrySet();
		Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
		Arrays.sort(entries, new Comparator() {
			public int compare(Object arg0, Object arg1) {
				Long key1 = Long.valueOf(((Map.Entry) arg0).getValue().toString());
				Long key2 = Long.valueOf(((Map.Entry) arg1).getValue().toString());
				return key1.compareTo(key2);
			}
		});

		return entries;
	}
	
	@SuppressWarnings("unchecked")
	public static void writeProperties(String filePath, Map<String, String> map) {
		Properties prop = new Properties();
		InputStream fis = null;
		OutputStream fos = null;
		try {
			fis = new FileInputStream(filePath);
			prop.load(fis);
			fos = new FileOutputStream(filePath);
			
			Set set = map.entrySet();
			Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
			
			for (int i = 0; i < entries.length; i++) {
				String key = (String)entries[i].getKey();
				String value = (String)entries[i].getValue();
				prop.setProperty(key, value);
				prop.store(fos, "Update '" + value + "' value");
			}
			fos.flush();
		} catch (IOException e) {
			System.err.println("error occurred");
		} finally {
			try {
				fos.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

注意读取与保存可能无法在MyEclipse中连贯,保存写入时info.properties文件在根目录,而读取时须在类文件夹下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值