Load and save properties to files. : Properties « Development Class « Java






Load and save properties to files.

      
/*
 * Copyright Javelin Software, All rights reserved.
 */


import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

/**
 * PropertiesUtil is used to load and save properties to files.
 * <p>
 * @author Robin Sharp
 */

public class PropertiesUtil
{

    public PropertiesUtil()
    {
    }

    public static Map getByValue( Properties properties, String value )
    {
        Map map = new HashMap();
        Iterator iter = properties.entrySet().iterator();
        do
        {
            if(!iter.hasNext() )
                break;
            java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();
            if(entry.getValue().equals(value ) )
                map.put(entry.getKey(), entry.getValue() );
        } while(true );
        return map;
    }

    public static Properties load( Properties properties, String fileName ) throws IOException
    {
        if(fileName == null )
        {
            throw new IllegalArgumentException( "FileName is not set." );
        }
        else
        {
            return load( properties, new File(fileName ) );
        }
    }

    public static Properties load( Properties properties, File file ) throws IOException
    {
        if(file == null )
        {
            throw new IllegalArgumentException( "File is not set." );
        }
        else
        {
            return load( properties, ((InputStream ) ( new FileInputStream(file ) )) );
        }
    }

    public static Properties load( Properties properties, URL url ) throws IOException
    {
        if( url == null )
        {
            throw new IllegalArgumentException( "Url is not set." );
        } 
        else
        {
            URLConnection connection = url.openConnection();
            return load( properties, connection.getInputStream() );
        }
    }

    public static Properties load( Properties properties, Class relativeClass, String relativeFileName ) throws IOException
    {
        if(relativeClass == null )
        {
            throw new IllegalArgumentException( "Relative Class is not set." );
        }
        if(relativeFileName == null )
        {
            throw new IllegalArgumentException( "Relative File Name is not set." );
        }
        else
        {
            return load( properties, relativeClass.getResourceAsStream(relativeFileName ) );
        }
    }

    public static Properties load( Properties properties, InputStream inputStream ) throws IOException
    {
        try
        {
            if( properties == null )
            {
                throw new IllegalArgumentException( "Properties is not set." );
            }
            
            if( inputStream == null )
            {
                throw new IllegalArgumentException( "InputStream is not set." );
            }
            
            properties.load( new BufferedInputStream(inputStream ) );
        }
        finally
        {
            if(inputStream != null )
                inputStream.close();
        }
        return properties;
    }

    public static void store( Properties properties, String fileName ) throws IOException
    {
        if(fileName == null )
        {
            throw new IllegalArgumentException( "FileName is not set." );
        } 
        else
        {
            store( properties, new File(fileName ) );
            return;
        }
    }

    public static void store( Properties properties, File file )
        throws IOException
    {
        if(file == null )
        {
            throw new IllegalArgumentException( "File is not set." );
        } else
        {
            store( properties, ((OutputStream ) ( new FileOutputStream(file ) )) );
            return;
        }
    }

    public static void store( Properties properties, URL url ) throws IOException
    {
        if( url == null )
        {
            throw new IllegalArgumentException( "Url is not set." );
        } else
        {
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true );
            store( properties, connection.getOutputStream() );
            return;
        }
    }

    public static void store( Properties properties, Class relativeClass, String relativeFileName )
        throws IOException
    {
        if(relativeClass == null )
            throw new IllegalArgumentException( "Relative Class is not set." );
        if(relativeFileName == null )
        {
            throw new IllegalArgumentException( "Relative File Name is not set." );
        } 
        else
        {
            String className = relativeClass.getName().substring(relativeClass.getName().lastIndexOf( "." ) + 1);
            URL url = relativeClass.getResource(String.valueOf(String.valueOf( className ) ).concat( ".class" ) );
            String fileName = url.getFile();
            fileName = String.valueOf(fileName.substring(1, fileName.length() - className.length() - 6) ) + String.valueOf(relativeFileName );
            store( properties, fileName );
            return;
        }
    }

    public static void store( Properties properties, OutputStream outputStream ) throws IOException
    {
        try
        {
            if( properties == null )
            {
                throw new IllegalArgumentException( "Properties is not set." );
            }
            if( outputStream == null )
            {
                throw new IllegalArgumentException( "OutputStream is not set." );
            }
            properties.store( new BufferedOutputStream( outputStream ), null );
        }
        finally
        {
            if( outputStream != null )
            {
                outputStream.close();
            }
        }
    }

    public static void main(String args[])
    {
        try
        {
            System.out.println(load( new Properties(), "C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) );
            System.out.println(load( new Properties(), new File( "C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) ));
            System.out.println(load( new Properties(), new URL( "file:///C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) ));
            System.out.println(load( new Properties(), PropertiesUtil.class, "test.properties" ) );
            Properties properties = new Properties();
            properties.put( "1", "2" );
            properties.put( "2", "2" );
            properties.put( "3", "3" );
            store( properties, "C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test1.properties" );
            store( properties, new File( "C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test2.properties" ) );
            store( properties, PropertiesUtil.class, "test4.properties" );
            System.out.println(getByValue( properties, "2" ) );
        }
        catch(Exception e )
        {
            e.printStackTrace();
        }
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Read a set of properties from the received input stream, strip off any excess white space that exists in those property values,
2.Copy a set of properties from one Property to another.
3.Sorts property list and print out each key=value pair prepended with specific indentation.
4.Sorts a property list and turns the sorted list into a string.
5.A utility class for replacing properties in strings.
6.Property Loader
7.Property access utility methods
8.Represents a persistent set of properties
9.Converts specified map to java.util.Properties
10.A java.util.Properties class that will check a file or URL for changes periodically
11.Encapsulates java.util.Properties to add java primitives and some other java classes
12.Adds new properties to an existing set of properties
13.Extracts a specific property key subset from the known properties
14.Observable Properties
15.Merge Properties Into Map
16.XML configuration management
17.JDOM based XML properties
18.XML Properties
19.Converts Unicode into something that can be embedded in a java properties file
20.Create Properties from String array
21.Task to overwrite Properties
22.Gets strong-type-value property from a standard Properties
23.The properties iterator iterates over a set of enumerated properties.
24.An utility class to ease up using property-file resource bundles.
25.Sorted Properties
26.A subclass of the java.util.Properties class that must be initialized from a file on disk
27.This class contains a collection of static utility methods for creating, retrieving, saving and loading properties.
28.Simplify routine job with properties
29.Property Manager