Class Properties

  • Direct Known Subclasses:
    MailSession, Options

    public class Properties
    extends java.lang.Object
    Used to store properties pairs (key,value). A hashtable is used to store them. Currently, the key must be a String and the value must be a Value The properties can be saved and loaded to/from a DataStream. Here's a sample:
       Properties props = new Properties();
       File file;      
       //***************************************
       // load properties that already exists
       //***************************************
       if (existe_arquivo(arquivo))
       {
          file = new File(arquivo, File.READ_WRITE);
          props.load(new DataStream(file));
          file.close();
       }     
       //***************************************
       props.put(propriedade,new Properties.Str(valor));
       file = new File(arquivo, File.CREATE);
       props.save(new DataStream(file));
       file.close();     
     
    See Also:
    Properties.Value, Properties.Str, Properties.Int, Properties.Double, Properties.Boolean, Properties.Long
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Properties.Boolean
      Implements a value of type boolean
      static class  Properties.Double
      Implements a value of type double
      static class  Properties.Int
      Implements a value of type int
      static class  Properties.Long
      Implements a value of type long
      static class  Properties.Str
      Implements a value of type String
      static class  Properties.Value
      Represents a generic value that can be stored here.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_PROPS
      Avoids that the load method gets into an infinite loop if the file is empty or corrupted.
    • Constructor Summary

      Constructors 
      Constructor Description
      Properties()  
      Properties​(java.lang.String[] keys, Properties.Value[] values)
      Stores the given keys/values pairs in a new Properties.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears this property
      java.lang.StringBuffer dumpKeysValues​(java.lang.StringBuffer sb, java.lang.String keyvalueSeparator, java.lang.String lineSeparator)
      Dumps the keys and values into the given StringBuffer.
      Properties.Value get​(java.lang.String key)
      Get the value given the key from the hashtable that stores the properties.
      Vector getKeys()
      Returns a Vector with the current keys
      void load​(DataStream ds)
      Load all properties from the given DataStream.
      void load​(DataStream ds, boolean cleanBeforeLoad)
      Load properties from the given DataStream.
      void put​(java.lang.String key, Properties.Value v)
      Put the given key/value pair in the hashtable that stores the properties.
      void remove​(java.lang.String key)
      Remove a value from the property
      void save​(DataStream ds)
      Save all properties in the given DataStream
      int size()
      Returns the number of properties
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAX_PROPS

        public static int MAX_PROPS
        Avoids that the load method gets into an infinite loop if the file is empty or corrupted. This constant limits the number of properties to 1000. If you are saving more than 1000 props, just change this max value.
    • Constructor Detail

      • Properties

        public Properties()
      • Properties

        public Properties​(java.lang.String[] keys,
                          Properties.Value[] values)
        Stores the given keys/values pairs in a new Properties.
    • Method Detail

      • put

        public void put​(java.lang.String key,
                        Properties.Value v)
        Put the given key/value pair in the hashtable that stores the properties.
      • get

        public Properties.Value get​(java.lang.String key)
        Get the value given the key from the hashtable that stores the properties.
      • size

        public int size()
        Returns the number of properties
      • getKeys

        public Vector getKeys()
        Returns a Vector with the current keys
      • clear

        public void clear()
        Clears this property
      • remove

        public void remove​(java.lang.String key)
        Remove a value from the property
      • load

        public void load​(DataStream ds)
                  throws IOException
        Load all properties from the given DataStream. Before calling this method, be sure that there's something to be read (ie, that the file is not empty), otherwise it may run in an infinite loop and will freeze your device.
        Throws:
        IOException
      • load

        public void load​(DataStream ds,
                         boolean cleanBeforeLoad)
                  throws IOException
        Load properties from the given DataStream. If cleanBeforeLoad is true, the contents of this object will be cleared before reading from the DataStream.
        Parameters:
        ds -
        cleanBeforeLoad -
        Throws:
        IOException
      • dumpKeysValues

        public java.lang.StringBuffer dumpKeysValues​(java.lang.StringBuffer sb,
                                                     java.lang.String keyvalueSeparator,
                                                     java.lang.String lineSeparator)
        Dumps the keys and values into the given StringBuffer. The values are dumped as Strings, exactly like the Hashtable implementation.
        Parameters:
        sb - The StringBuffer where the data will be dumped to
        keyvalueSeparator - The separator between the key and the value (E.G.: ": ")
        lineSeparator - The separator placed after each key+value pair (E.G.: "\r\n"). The last separator is cut from the StringBuffer.
        Returns:
        the received StringBuffer sb
        Since:
        TotalCross 1.25