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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » PropertiesScreenshots 
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 getByValueProperties 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 loadProperties properties, String fileName throws IOException
    {
        if(fileName == null )
        {
            throw new IllegalArgumentException"FileName is not set." );
        }
        else
        {
            return loadproperties, new File(fileName ) );
        }
    }

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

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

    public static Properties loadProperties 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 loadproperties, relativeClass.getResourceAsStream(relativeFileName ) );
        }
    }

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

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

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

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

    public static void storeProperties 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.valueOfclassName ) ).concat".class" ) );
            String fileName = url.getFile();
            fileName = String.valueOf(fileName.substring(1, fileName.length() - className.length() 6) ) + String.valueOf(relativeFileName );
            storeproperties, fileName );
            return;
        }
    }

    public static void storeProperties properties, OutputStream outputStream throws IOException
    {
        try
        {
            ifproperties == null )
            {
                throw new IllegalArgumentException"Properties is not set." );
            }
            ifoutputStream == null )
            {
                throw new IllegalArgumentException"OutputStream is not set." );
            }
            properties.storenew BufferedOutputStreamoutputStream )null );
        }
        finally
        {
            ifoutputStream != null )
            {
                outputStream.close();
            }
        }
    }

    public static void main(String args[])
    {
        try
        {
            System.out.println(loadnew Properties()"C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) );
            System.out.println(loadnew Properties()new File"C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) ));
            System.out.println(loadnew Properties()new URL"file:///C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test.properties" ) ));
            System.out.println(loadnew Properties(), PropertiesUtil.class, "test.properties" ) );
            Properties properties = new Properties();
            properties.put"1""2" );
            properties.put"2""2" );
            properties.put"3""3" );
            storeproperties, "C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test1.properties" );
            storeproperties, new File"C:/data/ccm_wa/dordev/Countrywide~csdrzs/Countrywide/src/com/javelin/util/test2.properties" ) );
            storeproperties, PropertiesUtil.class, "test4.properties" );
            System.out.println(getByValueproperties, "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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.