【问题标题】:Unordered Output of property file属性文件的无序输出
【发布时间】:2015-02-18 00:30:29
【问题描述】:
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 新手,我正在尝试导入一个包含大量键值对的属性文件,在上面的代码中,我能够导入数据,但导入的数据是我想要的非常无序的方式从上到下获取数据,以便我可以在其他类中使用它来实现某些功能,

谢谢

【问题讨论】:

    标签: java properties properties-file


    【解决方案1】:

    使用java.util.Properties 是不可能的。您要么需要自定义解析器、不同的文件格式,要么需要在键中放置一些可排序的前缀(001.key1=value002.key2=value 等),然后创建 new TreeMap&lt;&gt;(properties) 并对其进行迭代。

    【讨论】:

    • 我给了 001.key1=value1 并尝试了但还是一样,我是否也需要在我的代码中调用那些 001 订单?
    • 是的,例如,您必须在使用TreeMap 加载属性后对属性进行实际排序(我已经更新了我的答案)。
    • @bkail treesort 将进行字符串比较。但问题是保持插入顺序。
    • @SivaKumar 正如我在回答中所说,这是不可能的。
    • @bkail 有没有其他方法可以对它进行排序?我的意思是除了属性文件可以使用 .txt 或 excel 文件来获取我的数据并排序?4
    【解决方案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());
        }
    }
    

    【讨论】:

    • 不保证这将有效(或将继续有效)。 loadstore 方法的实现可能直接在底层 Hashtable 上运行,而无需使用您重载的方法。
    • 你确定我会在我的代码中实现并把你ping回来:)
    • @sankar 我应该在公共无效列表(PrintWriter out)方法中加载我的属性文件(object.properties)?
    • 是的..您可以使用和测试。
    • @sankar 它仍然是无序的:(
    【解决方案3】:

    谢谢大家,我有一个明确的解决方案,并想到在这里分享,以便每个人都可以使用

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

    使用集合我得到所有的键并将其存储在一个列表中并对我从我的属性文件中获得的整个数据进行排序

    【讨论】:

      猜你喜欢
      • 2011-12-26
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多