properties文件的存取与Map键值对排序【按照value进行排序】

文章来源:http://dracularking.iteye.com/blog/550388


Java代码   收藏代码
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.util.Arrays;  
  7. import java.util.Comparator;  
  8. import java.util.Enumeration;  
  9. import java.util.HashMap;  
  10. import java.util.Map;  
  11. import java.util.Properties;  
  12. import java.util.ResourceBundle;  
  13. import java.util.Set;  
  14.   
  15. public class Test {  
  16.   
  17.     public static void main(String[] args) {  
  18.         Map<String, String> map1 = new HashMap<String, String>();  
  19.         map1.put("aa""1");  
  20.         map1.put("bb""3");  
  21.         map1.put("cc""2");  
  22.         writeProperties("info.properties", map1);  
  23.           
  24.         Map map = getBundleInfoMap("Info");  
  25.           
  26.         Map.Entry<String, Integer>[] entry = getSortedHashtableByValue(map);  
  27.           
  28.         for (int i = 0; i < entry.length; i++) {  
  29.             System.out.println(entry[i]);  
  30.         }  
  31.           
  32.     }  
  33.       
  34.     public static Map<String, Integer> getBundleInfoMap(String bundleName) {  
  35.         ResourceBundle rb = ResourceBundle.getBundle(bundleName);  
  36.         Map<String, Integer> map = new HashMap<String, Integer>();  
  37.           
  38.         Enumeration<String> en = rb.getKeys();  
  39.           
  40.         while(en.hasMoreElements()) {  
  41.             String key = en.nextElement();  
  42.             map.put(key, Integer.valueOf(rb.getString(key)));  
  43.         }  
  44.         return map;  
  45.     }  
  46.   
  47.     /** 
  48.      * @param map 
  49.      * @return the entries of the map after being sorted 
  50.      */  
  51.     @SuppressWarnings("unchecked")  
  52.     public static Map.Entry[] getSortedHashtableByValue(Map<String, Integer> map) {  
  53.         Set set = map.entrySet();  
  54.         Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);  
  55.         Arrays.sort(entries, new Comparator() {  
  56.             public int compare(Object arg0, Object arg1) {  
  57.                 Long key1 = Long.valueOf(((Map.Entry) arg0).getValue().toString());  
  58.                 Long key2 = Long.valueOf(((Map.Entry) arg1).getValue().toString());  
  59.                 return key1.compareTo(key2);  
  60.             }  
  61.         });  
  62.   
  63.         return entries;  
  64.     }  
  65.       
  66.     @SuppressWarnings("unchecked")  
  67.     public static void writeProperties(String filePath, Map<String, String> map) {  
  68.         Properties prop = new Properties();  
  69.         InputStream fis = null;  
  70.         OutputStream fos = null;  
  71.         try {  
  72.             fis = new FileInputStream(filePath);  
  73.             prop.load(fis);  
  74.             fos = new FileOutputStream(filePath);  
  75.               
  76.             Set set = map.entrySet();  
  77.             Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);  
  78.               
  79.             for (int i = 0; i < entries.length; i++) {  
  80.                 String key = (String)entries[i].getKey();  
  81.                 String value = (String)entries[i].getValue();  
  82.                 prop.setProperty(key, value);  
  83.                 prop.store(fos, "Update '" + value + "' value");  
  84.             }  
  85.             fos.flush();  
  86.         } catch (IOException e) {  
  87.             System.err.println("error occurred");  
  88.         } finally {  
  89.             try {  
  90.                 fos.close();  
  91.                 fis.close();  
  92.             } catch (IOException e) {  
  93.                 e.printStackTrace();  
  94.             }  
  95.         }  
  96.     }  
  97. }  

 

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


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值