Java Properties Save saveToFile(Properties prop, String fileName)

Here you can find the source of saveToFile(Properties prop, String fileName)

Description

save To File

License

Apache License

Declaration

public static void saveToFile(Properties prop, String fileName) 

Method Source Code

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

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class Main {
    public static void saveToFile(Properties prop, String fileName) {
        try {/*from  w w  w  .ja  v a 2s  .  c  o  m*/
            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);
        }
    }
}

Related

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