Search code examples
javatreemapproperties-file

Ordering keys in .properties file alphabetically while ignoring case sensitivity


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


Solution

  • 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();
        }
    }