0

I have this function that stores values from a .properties file into a tree map (translatedMap), then retrieves new values from "keyMap" and stores them into "translatedMap" as well. The issue is no matter what I do it seems to always separate capitalized keys from non-capitalized keys. Here is my code:

Properties translation = new Properties(){

        private static final long serialVersionUID = 1L;

        @Override
        public synchronized Enumeration<Object> keys() {

            return Collections.enumeration(new TreeSet<Object>(super
                    .keySet()));
        }

    };


    //creates file and stores values of keyMap into the file
    try {
        TreeMap<String, String> translatedMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

        InputStreamReader in = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
        translation.load(in);

        // Store all values to TreeMap and sort
        Enumeration<?> e = translation.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            if (key.matches(".#")) {
            } else {
                String value = translation.getProperty(key);
                translatedMap.put(key, value);
            }
        }

        // Add new values to translatedMap
        for (String key : keyMap.keySet()) {
            // Handle if some keys have already been added; delete so they can be re-added
            if (translatedMap.containsKey(key)) {
                translatedMap.remove(key);
            }
            translatedMap.put(key, keyMap.get(key));
        }

        in.close();
        translation.putAll(translatedMap);
        File translationFile = new File(filePath);

        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(translationFile, false), "UTF-8");
        translation.store(out, null);

        out.close();
    } catch (IOException e) {
            e.printStackTrace();
    }
}

The output I'm getting is something like:

CAPITALIZED_KEY1=value1
CAPITALIZED_KEY2=value2
alowercase.key=value3
anotherlowercase.key=value4
morelowercase.keys=value5

When I would want it to come out like:

alowercase.key=value3
anotherlowercase.key=value4
CAPITALIZED_KEY1=value1
CAPITALIZED_KEY2=value2
morelowercase.keys=value5

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
Luke Gaskell
  • 175
  • 4
  • 15
  • I'm not sure I understand your issue. Can you please give a brief example of what's happening vs. what you're expecting to happen? – Ray Jun 18 '15 at 13:21
  • Please write the output you are getting and the desired output. – user3337714 Jun 18 '15 at 13:23

3 Answers3

1

Properties are not ordered. It doesn't matter what order you insert into them or if you call putAll() with something that is sorted, they extend Hashtable. See here.

Community
  • 1
  • 1
user1675642
  • 747
  • 2
  • 5
  • 15
1

The basic problem is that - though sorted case-insensitive -, an ordered map should still be case-sensitive as property names are case-sensitive.

Hence overide Properties, and on writing sort the names case-insensitive.

public class SortedProperties extends Properties {

    @Override
    public void store(Writer writer, String comments)
            throws IOException {
        List<String> names = new ArrayList<>();
        for (Enumeration<?> en = propertyNames(); en.hasMoreElements(); ) {
            String name = en.nextElement().toString();
            names.add(name);
        }
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compareTo(String other) {
                toLowerCase().compareTo(other.toLowerCase());
            }
        });
        //... write all properties
    }
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Yes it is a bit cumbersome, and my answer is rather rudimentary. – Joop Eggen Jun 19 '15 at 14:12
  • I couldn't seem to get overriding the store function to work... so i ended up going with something like this.. is there any problem doing it this way? So i just realized i can't post code in comments, sorry for all the edits, – Luke Gaskell Jun 19 '15 at 14:13
  • The only problem is that a key `AAA` and a key `aaa` then are equal, overwriting one the other yielding a single entry. However you might use a simple HashMap to store, and afterwards sort a copy of the `keySet()` – Joop Eggen Jun 19 '15 at 14:25
1

To achieve this I ended up avoiding the store function all together. I did the sorting inside the treeMap. I used a buffered writter and wrote to the file. like this:

Properties translation = new Properties();


    //creates file and stores values of keyMap into the file
    try {
        TreeMap<String, String> translatedMap = new TreeMap<String, String>(new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.toLowerCase().compareTo(o2.toLowerCase());
            }
        });

        InputStreamReader in = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
        translation.load(in);

        // Store all values to TreeMap and sort
        for (String key : translation.stringPropertyNames()) {
            keyMap.put(key, translation.getProperty(key));
        }

        in.close();


        Iterator<String> it = keyMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            translatedMap.put(key, keyMap.get(key));
        }

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, false), "UTF-8"));

        bw.write("#" + new Date().toString());
        bw.newLine();

        Iterator<String> it2 = translatedMap.keySet().iterator();
        while (it2.hasNext()) {
            String key = it2.next();
            bw.write(key + '=' + translatedMap.get(key));
            bw.newLine();
        }


        bw.close();
    } catch (IOException e) {
            e.printStackTrace();
    }
}
Luke Gaskell
  • 175
  • 4
  • 15