KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Properties

java.util
Class Properties

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<Object,Object>
          extended by java.util.Properties
All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>
Direct Known Subclasses:
Provider
See Also:
Top Examples, Source Code, storeToXML(OutputStream, String, String), loadFromXML(InputStream), store, load

protected Properties defaults
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1288]Use the default set of properties
By Anonymous on 2005/02/01 14:56:07  Rate
//You should note that java.util.Properties has a protected data  
 //member "protected Properties defaults". This is for a default set  
 //of properties. but since you cannot directly set a protected data  
 //member, you can extend java.util.Properties like the following 
 // manner:  
  
  
  
  
 import java.util.*; 
  
  
 public class Prop1 extends java.util.Properties 
  {  
     public Prop1 (  )  
      {  
         super (  ) ; 
      }  
     public void setDefault ( Properties defProp )  
      {  
         defaults = defProp; 
      }      
  }  
  
  
 //usage ... 
  
  
 Prop1 prop = new Prop1 (  ) ; 
 prop.setDefault (  ( java.util.Properties )  defProp ) ; 
 prop.put ( "HEIGHT","300" ) ; 
 prop.put ( "WIDTH","300" ) ; 
 propFileName = "prop.prp"; 
 comment = "APP SETTINGS, //date will be different for you  
 of course"; 
 outFile = new java.io.File ( propFileName ) ; 
 try 
  {  
      out = new java.io.FileOutputStream ( outFile ) ; 
      prop.save ( out, comment ) ; 
  }  
 catch ( java.io.IOException ioe )  { /*handle exception*/ }  
 finally 
  {  
     try { out.flush (  ) ;out.close (  ) ; }  
     catch ( Exception e )  { /*handle exception*/ }  
     out = null;             
  }  
 /* 
 now, if you look at prop.prp in your app directory,  
 you see         
 #APP SETTINGS, //the date will be different for you  
 of course 
 #Fri Sep 24 09:51:24 EDT 1999 
 HEIGHT=300 
 WIDTH=300 
 */
         
 


[1343]Read a property
By Prasad DLV on 2005/03/12 06:22:56  Rate
1.Create a 'test.properties' file with the following content. 
 name=dlv 
 dob=23081974 
 ----------------------------------------------------------- 
 2.use the following program to read the properties file. 
  
  
 import java.io.*; 
 import java.util.Properties; 
  
  
 public class PropertiesTest {  
 public static void main ( String [  ]  args )  {  
  
  
   Properties props=new Properties (  ) ; 
  
  
   try {  
     props.load (  new FileInputStream ( new File ( "test.properties" )  )  ) ; 
    } catch ( IOException ie )  {  
     System.out.println ( "Error reading file" ) ; 
    }  
   String key = "name"; 
   String val = props.getProperty ( key ) ; 
  
  
   System.out.println ( val ) ; 
  
  
  }  
  }  
  
  
 ------------------------------------------------ 
 3.When you run this program, you get the output as: 
 dlv


[1489]Print all the properties
By javabyexample on 2005/07/20 14:45:13  Rate
  public static void main ( String [  ]  args )  throws FileNotFoundException,IOException  {  
  
  
 java.util.Properties props = new java.util.Propertie (  ) ; 
 FileInputStream input; 
   props.load (  input = new FileInputStream (  ( "C:/prop.properties" )  )  ) ; 
 Enumeration enum = props.keys (  ) ; 
 while ( enum.hasMoreElements (  )  )  {  
 System.out.println ( "Element is..."+props.get ( enum.nextElement (  )  )  ) ; 
            }  
 props.getProperty ( key ) ; 
 input.close (  ) ; 
  
  
  }  
  
  
 //the properties file cannot have duplicate keys, if it has the last one will be output, the followin is in the file 
  
  
 ranaa=1 
 ranaf=2 
 ranad=3 
  
  
      
          } 


public String getProperty(String key)
See Also:
defaults, setProperty(java.lang.String, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[188]Get a property value from a property file in your working directory
By Sgt { dot } Pepper on 2004/12/13 06:08:45  Rate
//some property file in your working dir. 
 props.load (  new FileInputStream ( new File ( "test.properties" )  )  ) ; 
 String key = "some.key.in.above.file"; 
 String val = props.getProperty ( key ) ; 
 //thats it!


public String getProperty(String key,
                          String defaultValue)
See Also:
defaults, setProperty(java.lang.String, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1021]Properties Demo
By anandhasubha { at } yahoo { dot } com on 2004/10/28 15:27:48  Rate
import java.io.*; 
 import java.util.Properties; 
  
  
 public class PropertiesDemo 
  {  
   public static void main ( String args [  ]  )  
    {  
     Properties oProperties=new Properties (  ) ; 
     String strFileName = "CMQueries.properties"; 
  
  
     FileInputStream oFileStream = null; 
     try  {  
           oFileStream = new FileInputStream ( strFileName ) ; 
           oProperties.load ( oFileStream ) ; 
      }  catch  ( IOException e )   {  
       //handle error 
      }  
  
  
     System.out.println ( oProperties.getProperty ( "KeyNotFound","Default value for this key" )  ) ; 
      }  
  } 


public void list(PrintStream out)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[449]Print property list
By ceebee on 2003/11/20 18:02:56  Rate
props.list ( System.out ) ;

public void list(PrintWriter out)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void load(InputStream inStream)
          throws IOException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[300]Load a property file
By Anonymous on 2003/07/08 17:35:39  Rate
private Properties prop=new Properties (  ) ; 
 String filename = "aFile";            
  
  
 FileInputStream fStream = null; 
 try  {  
     fStream = new FileInputStream ( filename ) ; 
     prop.load ( fStream ) ; 
  }  catch  ( IOException e )   {  
   //some error handling here 
  }  finally  {  
     if  ( fStream != null )   {  
         try  {  
             fStream.close (  ) ; 
          }  catch  ( IOException e )   {  
             //some error handling here            
          }  
      }  
  } 


[777]Use ClassLoader to locate resources such as configuration file
By Anonymous on 2005/03/11 01:09:07  Rate
//Use ClassLoader to locate resources such as configuration file ( s )  
 // instead of using the absolute path to locate your property file 
 //PropertyLoader is the name of the class where this code is located.. 
  
  
 Properties props = new Properties (  ) ; 
 props.load ( newFileInputStream ( PropertyLoader.class.getClassLoader (  ) .getResourceAsStream ( "portCalc.properties" )  ) ;


[1968]How to load property settings with the Properties class in an WAR
By Anonymous on 2008/05/29 07:35:48  Rate
java.util.Properties objects can load values from a file using the method load ( InputStream ) . 
  
  
 Here is the code you need: 
  
  
 Properties props = new Properties (  ) ; 
 props.load ( new FileInputStream ( "propertyfile.properties" )  ) ; 
 String value = props.getProperty ( "propertyname" ) ; 
  
  
 //Just a trick: in a web archive  ( war )  you can get the InputStream inside the war archive using  
 ClassLoader cl = this.getClass (  ) .getClassLoader (  ) ; 
 InputStream is = cl.getResourceAsStream ( "it/package/application.properties" ) ; 
  
  
 This is better than using a FileInputStream, because you are loading the file within the archive as it was a resource. You should use this.getClass (  ) .getClassLoader (  )  to use the same ClassLoader as the one used the servlet container to load your JSP/Servlet. This code is snipped from a JSP page inside Tomcat.


public void loadFromXML(InputStream in)
                 throws IOException,
                        InvalidPropertiesFormatException
See Also:
storeToXML(OutputStream, String, String), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Properties()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[100]_
By Cheng on 2005/03/13 02:12:25  Rate
class abc 
  {  
   Properties prop = new Properties (  ) ; 
  
  
  } 


public Properties(Properties defaults)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration<?> propertyNames()
See Also:
defaults
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[450]Enumerate through properties
By ceebee on 2004/10/28 15:27:25  Rate
Enumeration enum = props.propertyNames (  ) ; 
 while (  enum.hasMoreElements (  )   )  
  {  
   String key =  ( String ) enum.nextElement (  ) ; 
   System.out.println (  key + "=" + props.getProperty (  key  )  ) ; 
  } 


@Deprecated
public void save(OutputStream out,
                            String comments)
See Also:
ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object setProperty(String key,
                          String value)
See Also:
getProperty(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1022]Change property value
By anandhasubha { at } yahoo { dot } com [Anandha Subha] on 2004/10/07 02:12:44  Rate
import java.io.*; 
 import java.util.Properties; 
  
  
 public class PropertiesDemo 
  {  
   public static void main ( String args [  ]  )  
    {  
     Properties oProperties=new Properties (  ) ; 
     String strFileName = "CMQueries.properties"; 
  
  
     FileInputStream oFileStream = null; 
  
  
     try  {  
           oFileStream = new FileInputStream ( strFileName ) ; 
            oProperties.load ( oFileStream ) ; 
  
  
      }  catch  ( IOException e )   {  
       //handle error 
      }  
  
  
     //System.out.println ( oProperties.getProperty ( "KeyNotFound","Default value for this key" )  ) ; 
     oProperties.setProperty ( "NEW_ENTRY","New value" ) ; 
     oProperties.list ( System.out ) ; 
      }  
  } 


public void store(OutputStream out,
                  String comments)
           throws IOException
See Also:
NullPointerException, ClassCastException, load
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[441]Update property file
By Anonymous on 2003/10/08 10:26:10  Rate
FileInputStream infile = new FileInputStream ( "test.properties" ) ; 
         FileOutputStream outfile = new FileOutputStream ( "test.properties" ) ; 
          
         prop.load ( infile ) ; 
          
         infile.close (  ) ; 
         prop.setProperty ( "key","value" ) ; 
  
  
         prop.store ( outfile,"header" ) ; 
         outfile.close (  ) ; 
 This will produce a file that looks like this 
 #header 
 #Wed Oct 08 09:11:04 CDT 2003 
 key=value


public void storeToXML(OutputStream os,
                       String comment)
                throws IOException
See Also:
loadFromXML(InputStream), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void storeToXML(OutputStream os,
                       String comment,
                       String encoding)
                throws IOException
See Also:
loadFromXML(InputStream), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags