2014-12-19 110 views
0
List<PData> listproperties = new ArrayList<PData>(); 
     try 
     { 
     PData PData = new PData(); 
     Properties properties = new Properties(); 
     File file = new File("src/Config/object.properties"); 
     FileInputStream Inputpropertyfile = new FileInputStream(file); 
     properties.load(Inputpropertyfile); 
     Inputpropertyfile.close(); 
     @SuppressWarnings("rawtypes") 
     Enumeration Keys = properties.keys(); 
     while(Keys.hasMoreElements()) 
     { 
      String key = (String) Keys.nextElement();//Getting Key from property file. 
       String Value = properties.getProperty(key);}} 

嗨的無序輸出,我是新來的Java我試圖導入具有巨大的設置鍵值對的屬性文件,在上面的代碼我能夠導入數據,但數據的輸入是非常無序的方式,我想在從上至下獲取數據,這樣我可以在其他類使用它的一些功能,屬性文件

謝謝

回答

2

這是不可能的使用java.util.Properties。你要麼需要一個定製的解析器,不同的文件格式,或者您需要把一些排序前綴鍵(001.key1=value002.key2=value等),然後創建一個new TreeMap<>(properties)和遍歷它來代替。

+0

了,我給喜歡001.key1 =值和嘗試,但它仍然是相同的,我需要調用這些001 ORD還有我的代碼? – user3724559 2014-12-19 04:48:37

+0

是的,例如(我已經更新了我的答案),您必須在使用'TreeMap'加載它們後實際對屬性進行排序。 – 2014-12-19 04:50:12

+0

@bkail treesort會做字符串比較。但問題是維護插入的順序。 – 2014-12-19 04:52:07

2

使用下面的代碼來維持秩序

import java.util.*; 
import java.io.*; 

/** 
* Ordered properties implementation 
*/ 

public class LinkedProperties extends Properties{ 
    private static final long serialVersionUID = 1L; 

    private Map<Object, Object> linkMap = new LinkedHashMap<Object,Object>(); 

    public void clear(){ 
     linkMap.clear(); 
    } 
    public boolean contains(Object value){ 
     return linkMap.containsValue(value); 
    } 
    public boolean containsKey(Object key){ 
     return linkMap.containsKey(key); 
    } 
    public boolean containsValue(Object value){ 
     return linkMap.containsValue(value); 
    } 
    public Enumeration elements(){ 
     throw new RuntimeException("Method elements is not supported in LinkedProperties class"); 
    } 
    public Set entrySet(){ 
     return linkMap.entrySet(); 
    } 
    public boolean equals(Object o){ 
     return linkMap.equals(o); 
    } 
    public Object get(Object key){ 
     return linkMap.get(key); 
    } 
    public String getProperty(String key) { 
     Object oval = get(key); //here the class Properties uses super.get() 
     if(oval==null)return null; 
     return (oval instanceof String) ? (String)oval : null; //behavior of standard properties 
    } 
    public boolean isEmpty(){ 
     return linkMap.isEmpty(); 
    } 
    public Enumeration keys(){ 
     Set keys=linkMap.keySet(); 
     return Collections.enumeration(keys); 
    } 
    public Set keySet(){ 
     return linkMap.keySet(); 
    } 
    public void list(PrintStream out) { 
     this.list(new PrintWriter(out,true)); 
    } 
    public void list(PrintWriter out) { 
     out.println("-- listing properties --"); 
     for (Map.Entry e : (Set<Map.Entry>)this.entrySet()){ 
      String key = (String)e.getKey(); 
      String val = (String)e.getValue(); 
      if (val.length() > 40) { 
       val = val.substring(0, 37) + "..."; 
      } 
      out.println(key + "=" + val); 
     } 
    } 

    public Object put(Object key, Object value){ 
     return linkMap.put(key, value); 
    } 
    public int size(){ 
     return linkMap.size(); 
    } 
    public Collection values(){ 
     return linkMap.values(); 
    } 

    //for test purpose only 
    public static void main(String[] arg)throws Exception{ 
     Properties p0=new Properties(); 
     Properties p1=new LinkedProperties(); 
     p0.put("aaa","111"); 
     p0.put("bbb","222"); 
     p0.put("ccc","333"); 
     p0.put("ddd","444"); 

     p1.put("aaa","111"); 
     p1.put("bbb","222"); 
     p1.put("ccc","333"); 
     p1.put("ddd","444"); 

     System.out.println("\n--"+p0.getClass()); 
     p0.list(System.out); 
     p0.store(System.out,"comments"); 
     p0.storeToXML(System.out,"comments"); 
     System.out.println(p0.toString()); 

     System.out.println("\n--"+p1.getClass()); 
     p1.list(System.out); 
     p1.store(System.out,"comments"); 
     p1.storeToXML(System.out,"comments"); 
     System.out.println(p1.toString()); 
    } 
} 
+0

這不能保證這將工作(或將繼續工作)。 'load'和'store'方法的實現可能直接在底層的'Hashtable'上運行,而不使用你重載的方法。 – 2014-12-19 05:20:29

+0

@sankar謝謝 – user3724559 2014-12-19 05:21:06

+1

如果working.please給予好評 – sankar 2014-12-19 05:23:14

0

謝謝大家,我得到的在這裏分享它一個明確的解決方案和思路,使每個人都可以利用

Enumeration Keys = properties.keys(); 
     List<String> list = Collections.list(Keys); 
     Collections.sort(list); 
     System.out.println(list); 
     Iterator<String> rows = list.iterator(); 

使用集合我得到所有的按鍵和將其存儲在列表和排序的整個數據,我從我的屬性文件