Java Utililty Methods Properties Save

List of utility methods to do Properties Save

Description

The list of methods to do Properties Save are organized into topic(s).

Method

voidsaveProps(Properties p, String fname, String comment)
save Props
FileOutputStream propertiesStream = new FileOutputStream(fname);
PrintStream os = new PrintStream(propertiesStream);
os.println("# " + comment);
os.println();
List<String> keys = new ArrayList<String>();
for (Object c : p.keySet())
    keys.add((String) c);
Collections.sort(keys);
...
booleansaveProps(String path, Properties props)
Save properties to file.
return saveProps(new File(path), props);
voidsaveSorted(Properties props, File file)
Sort and save properties to a file.
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() {
...
voidsaveSysDirProperties(Properties sysProps, String classpathDirectory)
save Sys Dir Properties
File sysdirPropsFile = new File(classpathDirectory, "sipxconfig.properties");
FileOutputStream sysdirPropsStream;
try {
    sysdirPropsStream = new FileOutputStream(sysdirPropsFile);
    sysProps.store(sysdirPropsStream, null);
} catch (FileNotFoundException e) {
    throw new RuntimeException("could not create system dir properties file", e);
} catch (IOException e) {
...
voidsaveToFile(Properties prop, String fileName)
save To File
try {
    FileOutputStream fileOutput = new FileOutputStream(fileName);
    OutputStreamWriter writer = new OutputStreamWriter(fileOutput, "ISO-8859-1");
    Enumeration propNames = prop.propertyNames();
    List keys = new ArrayList();
    while (propNames.hasMoreElements()) {
        keys.add(propNames.nextElement());
    Collections.sort(keys);
    for (int i = 0; i < keys.size(); i++) {
        String key = (String) keys.get(i);
        writer.write(key);
        writer.write(" = ");
        writer.write(prop.getProperty(key));
        writer.write("\n");
    writer.flush();
    writer.close();
    fileOutput.close();
} catch (FileNotFoundException e) {
    throw new RuntimeException(e);
} catch (IOException e) {
    throw new RuntimeException("Unexpected exception! caused by: " + e.getMessage(), e);
voidsaveUserSettings(Properties properties)
save User Settings
String filename = System.getProperty("user.home") + File.separator + ".RacePoint.xml";
OutputStream out = null;
try {
    out = new FileOutputStream(filename);
    BufferedOutputStream outBuf = new BufferedOutputStream(out);
    properties.storeToXML(outBuf, "User settings for RacePoint");
    outBuf.close();
} catch (IOException e) {
...
Stringserialize(Properties properties)
serialize
StringWriter outputWriter = new StringWriter();
properties.store(outputWriter, "");
String rst = outputWriter.toString();
outputWriter.close();
return rst;
Stringserialize(Properties props)
Serialize properties to a string suitable for a subsequent load()
ByteArrayOutputStream baos = new ByteArrayOutputStream();
props.store(baos, null);
return baos.toString();