Java Properties Save saveSorted(Properties props, File file)

Here you can find the source of saveSorted(Properties props, File file)

Description

Sort and save properties to a file.

License

Apache License

Parameter

Parameter Description
props Properties
file file

Exception

Parameter Description
Exception an exception

Declaration

public static void saveSorted(Properties props, File file) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.util.*;

public class Main {
    /**//w w w.  j ava  2s  .c  o  m
     * 
     * Sort and save properties to a file.
     * 
     * @param props Properties
     * @param file file
     * @throws Exception
     */
    public static void saveSorted(Properties props, File file) throws Exception {

        try (FileOutputStream fout = new FileOutputStream(file)) {
            Properties sorted = new Properties() {
                @Override
                public Set<Object> keySet() {
                    return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
                }

                @Override
                public synchronized Enumeration<Object> keys() {
                    return Collections.enumeration(new TreeSet<Object>(super.keySet()));
                }

                @Override
                public Set<String> stringPropertyNames() {
                    return Collections.unmodifiableSet(new TreeSet<String>(super.stringPropertyNames()));
                }
            };
            sorted.putAll(props);
            sorted.storeToXML(fout, null);
            fout.flush();
        }
    }
}

Related

  1. saveProperty(String key, String value)
  2. saveProperty(String key, String value)
  3. saveProperty(String key, String value)
  4. saveProps(Properties p, String fname, String comment)
  5. saveProps(String path, Properties props)
  6. saveSysDirProperties(Properties sysProps, String classpathDirectory)
  7. saveToFile(Properties prop, String fileName)
  8. saveUserSettings(Properties properties)
  9. serialize(Properties properties)