简体   繁体   中英

java write properties file

I have a treemap that has sorted information (I sorted by the hashmap's value, not key) but when I want to write them into the properties file, the order is not sequence. what is the problems? Can any one help me?

ArrayList<Integer> values = new ArrayList<Integer>();
values.addAll(wordcount.values());

Collections.sort(values, Collections.reverseOrder());

ValueComparator bvc = new ValueComparator(wordcount);
TreeMap<String, Integer> sorted_map = new TreeMap(bvc);
sorted_map.putAll(wordcount);

Properties props=new Properties();
FileInputStream fis=new FileInputStream("abc.properties");
props.load(fis);

for (Integer i : values) { 
    for (String s : sorted_map.keySet()) { 
        if (sorted_map.get(s) == i){
            props.setProperty(s, String.valueOf(i));
            props.store(new FileOutputStream("abc.properties"), null);
            break;
        }
    }
}

Here's an extension of Properties which will give you sorted keys, entries, toString() output, and file output via store():

import java.util.*;

/**
 * Extension of Properties with sorted keys for alphabetic output.
 * Neither optimized for performance nor thread-safety.
 */
public class SortedProperties extends Properties {

/**
 * Called throughout by Properties, including 
 *   Properties.store(OutputStream out, String comments).
 */
@Override
public synchronized Enumeration<Object> keys() {
    return new Vector(this.keySet()).elements();
}

/**
 * Called by Properties.stringPropertyNames() and this.keys().
 */
@Override
public Set<Object> keySet() {
    Set<Object> keySet = super.keySet();
    if(keySet==null) return keySet;
    return new TreeSet(keySet);
}

/**
 * Called by Properties.toString().
 */
@Override
public Set<Map.Entry<Object, Object>> entrySet() {
    Set<Map.Entry<Object, Object>> entrySet = super.entrySet();
    if (entrySet==null) return entrySet;

    Set<Map.Entry<Object, Object>> sortedSet = new TreeSet(new EntryComparator());
    sortedSet.addAll(entrySet);
    return sortedSet;
}

/**
 * Comparator for sorting Map.Entry by key
 * Assumes non-null entries.
 */
class EntryComparator implements Comparator<Map.Entry<Object, Object>> {

    @Override
    public int compare(Map.Entry<Object, Object> entry1, Map.Entry<Object, Object> entry2) {
        return entry1.getKey().toString().compareTo(entry2.getKey().toString());
    }

}

}

If you use java.util.Properties the key-value pairs are stored in a Hashmap which doesn't maintain any order no matter how you insert sorted values in a HashMap.

The only way to write them in a file keeping them sorted is to implement the output by your self.

By definition a TreeMap is sorted on its keys. You can make a new comparison function between keys, but you have to exchange role of key and value for what you want. Let K and V be the types of your keys and values.

TreeMap<V,K> tree = new TreeMap<V,K>()

for (Entry<K,V> entry : yourHashMap.entrySet())
 tree.put(entry.getValue(), entry.getKey()); // note swap

for (Entry<V,K> entry : tree.entrySet())
 properties.add(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM