5 Replies Latest reply on Nov 9, 2010 9:27 AM by cash1981

    Read Properties File

    karboe
      Hi all,
      I need to get some value from my properties file in my seam project
      and I put my properties file in the root path project
      In myproperties file, I write a property name  that I named myname with maurice as value.
      I has a reader properties file class as this following code:

      public class ReadPropertiesFile {                             
                      private Properties readPropertiesFile(String fileName) {
                              Properties properties = new Properties();
                              try {
                                      properties.load(new FileInputStream(fileName+".properties"));                          
                              } catch (FileNotFoundException e) {                    
                                      e.printStackTrace();                           
                              } catch (IOException e) {                      
                                      e.printStackTrace();                           
                              }
                              return properties;
                      }
                     
                      public String readPropertyValue(String propertyName) {                 
                              String propertyValue = null;
                              Properties properties = new Properties();
                              properties = readPropertiesFile("myproperties");
                              propertyValue = properties.getProperty(propertyName);
                              return propertyValue;
                      }
      }




      and then I try to get the myname property value with this following code

      ReadPropertiesFile readFile = new ReadPropertiesFile();
      String vProp = readFile.readPropertyValue("myname");

      It's always generate an error, where is the problem? 


      thanks in advance,
      Maurice.








        

        • 1. Re: Read Properties File
          asookazian

          use this, it works:


          import java.util.Enumeration;
          import java.util.Properties;
          import java.util.PropertyResourceBundle;
          import java.util.ResourceBundle;
          
          public class CoxProperties {
               private static Properties ijwatProps = null;
          
               public static Properties getPropertyObject(String configFileName) {
                    if (ijwatProps == null) {
                         ijwatProps = new Properties();
                         Enumeration<String> enumKeys;
                         ResourceBundle bundle = PropertyResourceBundle
                                   .getBundle(configFileName);
                         enumKeys = bundle.getKeys();
                         while (enumKeys.hasMoreElements()) {
                              String key = (String) enumKeys.nextElement();
                              ijwatProps.setProperty(key, (String) bundle.getString(key));
                         }
                    }
          
                    return ijwatProps;
               }
          
               public static Properties getPropertyObject() {
                    return getPropertyObject(CoxConstants.CONFIG_FILENAME);
               }
          
               public static String getProperty(String key) {
                    if (key == null)
                         return null;
          
                    return ijwatProps.getProperty(key);
               }
          
          }

          • 2. Re: Read Properties File
            cash1981

            Or much easier, you can define your properties in components*.properties.


            myproperty=someValue


            Then in your components.xml you can define then in the seam component you are using them.




            <component name="mySeamcomponent">
                      <property name="myproperty">@myproperty@</property>
                 </component>




            Then in your mySeamcomponent class




            // injected from components.xml
            private String myproperty;
            
            public getMyproperty();
            public setMypropert(String ...);
            
            



            • 3. Re: Read Properties File
              jjlang
              define your properties in components*.properties, if you use them in your page you can use like this " value="#{messages['key']}" "
              • 4. Re: Read Properties File

                Hi


                Can you let me know which method you are exposing to the bean, Mr. Arbi Sookazian.  That is if I have to instantiate CoxProperties and call the method on it, which method would I be calling.


                thanks
                Sai

                • 5. Re: Read Properties File
                  cash1981

                  andrew lee wrote on Aug 06, 2009 10:18:


                  define your properties in components.properties, if you use them in your page you can use like this


                  This is just plain wrong. You cannot use Messages to read the component.properties file. It will read the messages.properties file.