john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

PropertiesWriter

// 2012-11-30 johnpfeiffer

package kittyandbear.net;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class PropertiesWriter {

    public static final String CLASSVERSION = "0.30";
    static final String NEWLINE = System.getProperty("line.separator");
    private Properties properties;


    protected String getVersion() {
        return CLASSVERSION;
    }

    PropertiesWriter( String pathFile ) throws IOException, IllegalArgumentException {
        properties = new PropertiesReader( pathFile ).get();
    }

    public void setPropertyValue( String key, String value ) throws IllegalArgumentException {
        if (key == null) {
            throw new IllegalArgumentException( "ERROR: key cannot be null" );
        }
        if (key.isEmpty()) {
            throw new IllegalArgumentException( "ERROR: key cannot be empty" );
        }
        if (value == null) {
            throw new IllegalArgumentException( "ERROR: value cannot be null" );
        }
        properties.setProperty( key, value );
    }

    public void write( String pathFile ) throws IllegalArgumentException, IOException {
        if (pathFile == null) {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be null" );
        }
        if (pathFile.isEmpty()) {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be empty" );
        }
        if (properties == null) {
            throw new IllegalStateException( "ERROR: properties are null" );
        }

        BufferedWriter output = new BufferedWriter( new FileWriter( pathFile ) );
        properties.store( output , null );
        output.close();
    }

    public void writeSorted( String pathFile ) throws IllegalArgumentException, IOException {
        if (pathFile == null) {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be null" );
        }
        if (pathFile.isEmpty()) {
            throw new IllegalArgumentException( "ERROR: pathFile cannot be empty" );
        }
        if (properties == null) {
            throw new IllegalStateException( "ERROR: properties are null" );
        }

        BufferedWriter output = new BufferedWriter( new FileWriter( pathFile ) );

        ArrayList<String> sortedKeyList = new ArrayList<String>();
        Set<Object> set = properties.keySet();
        Iterator<Object> it = set.iterator();
        while( it.hasNext() ) {
            sortedKeyList.add( it.next().toString() );
        }
        Collections.sort( sortedKeyList ); // case sensitive so A before a

        StringBuilder strb = new StringBuilder();
        for (String key : sortedKeyList) {
            strb.append( key + "=" + properties.getProperty( key ) + NEWLINE );
        }

        output.write( strb.toString() );
        output.close();
    }

} // end class

  • « PropertiesReader pom.xml
  • PropertiesWriterTest »

Published

Dec 4, 2012

Category

java-classes

~233 words

Tags

  • classes 92
  • java 252
  • propertieswriter 2