无序输出属性文件

时间:2014-12-19 04:38:10

标签: java properties properties-file

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的新手我正在尝试导入一个具有大量键值对的属性文件,在上面的代码中我能够导入数据,但导入的数据是非常无序的,我想要从上到下获取数据,以便我可以在其他类中使用它来实现某些功能,

谢谢

3 个答案:

答案 0 :(得分:2)

使用java.util.Properties无法做到这一点。您需要自定义解析器,不同的文件格式,或者您需要在键中放置一些可排序的前缀(001.key1=value002.key2=value等),然后创建new TreeMap<>(properties)并迭代相反。

答案 1 :(得分: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());
    }
}

答案 2 :(得分:0)

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

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

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