Doubts in reading a '.properties' file

Hi All,
I have a doubt regarding properties file from which I am reading some key-values. Now I have few different keys (4-5) having same value .e.g.
key1=value1
key2=value1
key3=value1
key4=value1
key5=value1
Now I need to read these keys from different places and I also need to update the value to say 'value2' for all the keys e.g.
key1=value2
key2=value2
key3=value2
key4=value2
key5=value2
Now, since i need to update the values of all the keys, I am thinking of having another key-value pair (e.g. key=value1) in the same property file, and using the value of this key as value for all the keys displayed above. So that when I update the value of this key ( key=value2), I dont need to update the values of all the keys, since those keys will be reading the values from this key's value.
I hope i am clear with this explanation.... ;-)
My first question is, 'IS IT POSSIBLE' ???
If yes, then how????
Thanks in advance
Inder Jeet Singh

The following little class is from my old bag of tricks:import java.util.Properties;
public class XProperties extends Properties {
     public static final String BEG= "${";
     public static final String END= "}";
     public XProperties() { super(); }
     public XProperties(Properties def) { super(def); }
     public String getRawProperty(String key) { return getRawProperty(key, null); }
     public String getRawProperty(String key, String def) {
          return super.getProperty(key, def);
     public String process(String val) {
          for (int i, j, n= val.length(); n >= 0 && (i= val.lastIndexOf(BEG, n)) >= 0 &&
            (j= val.indexOf(END, i)) >= 0; ) {
               String pat= val.substring(i+BEG.length(), j);
               String sub= super.getProperty(pat);
               if (sub != null) {
                    val= val.replaceAll("\\Q"+BEG+pat+END+"\\E", sub);
                    n= val.length();
               else
                    n= i-1;
          return val;
     public String getProperty(String key) { return getProperty(key, null); }
     public String getProperty(String key, String def) {
          String val= super.getProperty(key);
          return val == null?def:process(val);
}If you use an XProperties object instead of a Properties object
you can do the following in your .properties file:key= value1
key1= ${key}
key2= ${key}
key3= ${key}
key4= ${key}
key5= ${key}... if you want to change the value, all you have to do is change the
value of the 'key=value1' line.
kind regards,
Jos

Similar Messages

  • How do I read a properties file in WEB-INF without hard-coding a path?

    Hello,
    How do I read a properties file in WEB-INF without hard-coding a path?
    I tried:
    Properties properties = new Properties();
    properties.load(new FileInputStream("db.properties"));
    driver = properties.getProperty("driver");
    but it cannot find the db.properties file.
    Thanks for the help.
    Frank

    Don't use a File to read those properties.
    Better to use the servlet context and
    getResourceAsStream() method to get the InputStream.
    It'll look for any file in the CLASSPATH. If you put
    that properties file in the WEB-INF/classes directory
    you'll have no problems, even if you deploy with a
    WAR file.Completely agree with this approach. Just have to mention the following for completeness
    according to the API,
    "This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. "
    So using this method, the resource can be anywhere under your web context, not just in the classpath.
    Cheers,
    evnafets

  • Reading a properties file in a servlet and passing its contents to a JSP pa

    Hi all,
    I'm totally new to Servlet pgmg n JSP. Please can ne1 tell how to read a simple properties file (in a servlet) and pass its contents to a JSP page.Actually the reason is to fill a combo box in a JSP page with the contents of a properties file.If calling a servlet for the same is d best way to do that, plz can ne1 tell me :
    1> whether to override the doPost method in the servlet in which to read d file using FileInputStream
    2> Putting the contents in an array and then how to pass this array to the JSP page.
    Thanks,
    deepthy.

    I'm using a properties file to let my web application know what the name of the database is. I'm using an abstract class GeneralDao which will be extended by all the DAO's (Data Access Objects or java classes containing SQL statements).
    Here's the code :
    protected Connection getDatabaseConnection()
              throws Melding
         Connection dbconn = null;     
         ResourceBundle resBundle;
         try
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         resBundle = ResourceBundle.getBundle("gcoConfig");
         String dbConnectie = resBundle.getString("databaseconnection");
         gcoLogger.debug("lezen databaseconnection in resourceBundle " );
         dbconn = DriverManager.getConnection(dbConnectie);
         } catch (InstantiationException exc)The ResourceBundle is used to read the properties file, named gcoConfig.properties.
    With the getString() command I read the content of the string named databaseconnection.
    The gcoConfig.properties file is placed inside the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes
    The GeneralDao is placed in the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes\org\gertcuppens\cluifDao.
    So this class is placed some folder below the WEB-INF\classes folder of gcoConfig.properties.
    And the content of the properties file is just one line :
    databaseconnection=jdbc:mysql://localhost/cluif
    If you want to let the JSP know what's inside the properties file, wrap the content in a Javabean, put it in the request or even the session and pass the control to the JSP.

  • Java.lang.NullPointerException   in the code to read a properties file

    I have written the following code to read a properties file...........
    <b>
    public Property(String fileName)
        try
           InputStream is = MyClassName.class.getClass().getClassLoader().getResourceAsStream(fileName);     
           prop.load(is);
           is.close();
        catch (Exception ex)
            System.out.println("The Exception is:        " + ex.getMessage());
            ex.printStackTrace();
    </b>
    But this code is giving <b> java.lang.NullPointerException </b> at <b> prop.load(is) </b>
    please help me....
    regards
    Brahmachaitanya

    hi,
        If you are loading a resource (property) file from a jar file you can use
    getResourceAsStream("com/test/test.properties").
        if the propery file is in outside of your jar file . you can use
    Property portalProps = new Property ();
    try {
                   FileInputStream fileInputStream = new FileInputStream("<FilePath + FileName>");
                   portalProps.load(fileInputStream);
                   fileInputStream.close();
              } catch (IOException e) {
                   //Log the error
    refer the following link for more details
    http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html

  • How to read the properties file available in Server File structure in webdy

    hi all,
    I have developed one webdynpro application. In this application i need to access mdm server to continue. For getting the connection i need to pass the IP addresses.
    Can i have code  how to read the properties file which is residing in the server file. with out included along with the application. keeping some where in the file structure in the server. I want to read that properties file by  maintain the iP addresses and users in  properties file based on the key i want to read like below.
    servername="abcServer"
    username="john"
    password="test123"
    Please send me the code how to read this properties file from the file structure and how to read this values by key  in webdynpro application
    Regards
    Vijay

    Hi Vijay,
    You can try this piece of code too:
    Properties props = new Properties();
    //try retrieve data from file
    //catch exception in case properties file does not exist
    try {
             props.load(new FileInputStream("c:\property\Test.properties")); //File location
             String serverName = props.getProperty("servername"); //Similarly, you can access the other properties
             if(serverName==null)
               //....do appropriate handling
         }     catch(IOException e)
                   e.printStackTrace();
    Regards,
    Alka.

  • Read a Properties File in a Static Floating Block

    I am trying to read a properties file in a static floating block:
       static
          try
             Properties p = new Properties();  
             String path = "/WEB-INF/classes";
             InputStream is = getClass().getResourceAsStream( path + "artimus_message_MessagInboxConfig.properties" );
             p.load(is); // load the stream into the Properties object
             WEBSITE_HOME = p.getProperty( "WEBSITE_HOME" );
             setWebSiteHome( WEBSITE_HOME );
          catch { ......}
       }I got a compilation error saying that the non-static method getClass() cannot be referenced from a static context. There is almost no way to make this static floating block non-static. What should I do?

    Thanks for the reply. But, huh... I do not quite
    understand. Which class is MyClass?It's a generic placeholder for whichever class your put that block into. You wouldn't put it there literally--replace it with the name of the actual class. If that block is in Foo.java, then use Foo.class instead of MyClass.class.
    >
    Okay, this static floating block is in a class called
    MessageInboxConfig.java. Then use MessageInboxConfig.class.getResourceAsStream(...)
    And inside this static
    floating block, I am reading a properties file and
    this properties file is in the
    ContextRoot/WEB-INF/classes/artimus_message_MessagInbox
    onfig.propertiesHow do I read this properties file?
    Properties p = new Properties();  
    InputStream is = MessageInboxConfig.class.getResourceAsStream("artimus_message_MessagInboxConfig.properties" );
    ... etc. ...You shouldn't need to specify "WEB-INF/classes," since it should be in your classpath. What you pass to getResource is a path relative to a directory in your classpath.

  • How to read from properties file

    Hi,
    I am using JSR 168.
    while creating a new portlet, a folder gets created with the name as "portlet". Under which is resource package and <PortletName>Bundle.java.
    pls tell me how to read from .properties file.
    waiting eagerly for some reply
    Thanks & Regards,
    HP
    Edited by: user9003827 on Apr 13, 2010 3:42 AM

    I think i have mixed it up :)
    I have looked at it again and believe you are using regular JSP portlets.
    Can you tell what you want to achieve by reading .properties file. Are you meaning the preferences of the portlet or what exactly are you trying to do?
    Reading propertie files is easy:
    // Read properties file.
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
        String myKey = properties.getProperty("yourKey");
    } catch (IOException e) {
    }Edited by: Yannick.O on 13-Apr-2010 05:52

  • How to read a properties file

    hi
    I have a properties file.
    I am using load method to load the properties file but i have encountered a problem here.
    the properties file has space like
    FIRST PROP=xyz
    SECOND PROP=abc
    my question is how to read the file
    thank u

    Either format it properly--I think a \ before the space will work--or write your own method to use in place of Properties.load.
    (Or, I suppose, you could inject your own InputStram that translates the lines on the fly to include the \, but that would probably be more trouble than it's worth.)

  • Help: Servlet can't read from properties file

    I am working on a web application running in Tomcat on Linux.My java source file reads some parameters from a properties file (data.properties)in the same directory. This works well in a simple application. However, if it is used in the Tomcat environment, it can't be read by a servlet file (LoginServlet.java). I put both the "LoginServlet.class" and "data.properties" in the following directory: /webapps/myapp/WEB-INF/classes. When I run the web application in Tomcat, the error is "data.properties not found". What is the problem? Do I need to modify my "web.xml" and/or Tomcat's "server.xml"? If so, how to modify them?
    This web application contains a lot of serlets. So I don't want to hardcode the parameters into the servlet codes.
    Any info will be appreciated. Thanks in advance!:)

    Putting the properties file in webapps/myapp may or may not help.
    My assumption is that you are trying to open the file by brute force, with something like
    InputStream propis = new FileInputStream ("data.properties");
    Properties props = new Properties ( );
    props.load (propis);
    What you want is to have the class loader find a file in the same place as the class file, and give you back an input stream reading from that file, which you can then pass to the Properties.load(InputStream) method. This is called a "resource," and the way to do it is:
    InputStream propis = getServletContext().getResourceAsStream("data.properties");
    // remaining code as above
    This will cause the class loader for the servlet context (a/k/a the Web application) to find the file in the same manner as it might find a class, but instead of treating it as compiled JVM code, and loading it, it simply passes you back an InputStream from which you do whatever you want.
    Cheers!
    Jerry Oberle
    perl -e 'printf "mailto%cg%s%cearthlink%cnet\n", 58, "oberle", 64, 46;'

  • Read resourceBundle properties file from KM

    Hi,
    I have a properties file called com.mycomp.apps.phonebook.properties in the KM Content of the portal in KM Content => root => mycomp => apps => phonebook => customizing
    Now I'm developing a new LinkCommand service for the UserDetails.
    http://help.sap.com/saphelp_nw04/helpdata/en/65/29b24053c13f5fe10000000a155106/frameset.htm
    The already existing LinkCommand "Send E-mail..." is read from the original resourceBundle com.sap.ip.collaboration.gwui.api.wdf.mail.SendTo
    For my new LinkCommand I would like to use the existing properties file. How do I have to define this file in the uicommand xml or properties xml sheet?
    Can anybody help me?
    With the local project resourceBundle it's working, but I would like to use the file in the KM. Andy ideas?
    Regards,
    Stefan

    Hi Praven,
    Copying all existing SendTo properties file to my project would be an option but I wouldn't win anything.
    I'm quite happy with the existing SAP collaboraton properties files using e.g. for "Send e-mail".
    I won't change this existing ones. I created a new LinkCommand. And for this new LinkCommand I'm looking for a solution how I can handle the translation for it.
    I would prefer e using my existing file which is located in the KM. The reason why it's located in the KM is, if I need to change a translation or add a new translation properties file I only need to change it in the KM. If it's a part of the project I need by every change to deploy the project new.

  • Read a properties file

    hi all,
    i do have a java class, it's a WebService in fact, coded in java. I must add something, and as i learned by myself java there some things i don't know.
    my class must access to a properties file to store data like server, dbs path etc... this file will also be changeable by an administrator on the machine
    i'have seen that with java.util.Properties there is a method to save the properties into an outputstream (consequently into a file i guess), but i have never dealed with output and files, so i'm a bit lost, could someone give me some clues, or a link to some code ?
    regards

    Here is some code. These methods just override methods in java.util.Properties with a new instance of a Properties-like class called MyProperties. To make an instance of this class, just pass in the file name into the constructor. THIS CODE WON'T WORK BY ITSELF AND YOU WILL NEED TO MAKE MODIFICATIONS! It's just some old code i pulled down from an old app i wrote. A better way to do this would be just extending from java.util.Properties, but i don't have time to fix it.
    public MyProperties (String name) throws FileNotFoundException,IOException {
              this.name = name;
    //creates input stream of the file you passed in
              inFile = new BufferedInputStream(new FileInputStream(name));
    //instance of java.util.properties
              prop = new Properties();
              prop.load(inFile);
         //containsKey wrapper
         public boolean containsKey(String key) {                                   //'key' is the key you search for in the 'name' file
              return prop.containsKey(key);
         //saves the passed value to the specified key (writes to the file). Define 'header' to add a header message at the beginning of the file.
         public void store(String header) throws IOException {
    //your output file...in this case, it's also the input file     
    BufferedOutputStream outFile = new BufferedOutputStream(new FileOutputStream(name));
              prop.store(outFile,header);
              return;
         // setProperty
         public void setProperty(String key, String attribute) {                    //sets the key 'key' to the value 'attribute'
    //before writing to the file, YOU should check whether the user has admin privileges!     
    prop.setProperty(key, attribute);
         // getProperty
         public String getProperty(String key) {                                        //gets the value of the key 'key'
         String theAttribute = prop.getProperty(key);
         return theAttribute;

  • Doubt on writing a properties file in to jar file

    hi,
    I have a jar file. In that jar file, I have a folder, named '/data/'. In this data folder i want to write a properties file. how to write?
    i was tried using the store method but i did not get it
    can any one help me out.
    thank you.

    You want to place the log4j.properties inside the .jar? You can read the log4j.properties inside the jar and give the Properties to log4j like this:
    String propertiesFile= "pathInJar/log4j.properties";
    InputStream in = getClassLoader().getResourceAsStream(propertiesFile);
    if (in != null)
        Properties props = new java.util.Properties();
        props.load(in);
        PropertyConfigurator.configure(props);
    else
       //PropertiesFile not found! (Handle the problem)
    }

  • I got ZipException to read Jndi.Properties file

    hello friends,
    here i got ZipException to get Jndi.Properties file.
    i place Jndi.Properties file in my directory and i set classpath to weblogic.jar&Jndi.Properties file.
    but i didn't get InitialContext obj.though i got zipException.
    jvm displays Jndi.Properties file is not read.

    To try out I pasted the code given by u in my application as a demo
         private static Connection connect() throws Exception{
         InputStream in = QueryTest.class.getResourceAsStream("JDBC.properties");
              Properties props = new Properties();
              props.load(in);
              //in.close();
              String driver = props.getProperty("JDBC.driver");
              System.out.println(driver);
              String url = props.getProperty("JDBC.url");
              System.out.println(url);
              String user = props.getProperty("JDBC.user");
              System.out.println(user);
              String pwd = props.getProperty("JDBC.password");
              System.out.println(pwd);
              in.close();
              Class.forName(driver);
              return DriverManager.getConnection(url, user, pwd);
         public static void main(String[] args) throws Exception{
              Connection con = connect();
              //Put your code here
    JDBC.properties file is
         url = jdbc:mysql://localhost:3306/mysql
              user= root
                   password =password
                   driver=com.mysql.jdbc.Driver
    And the error messages are
    null
    null
    null
    null
    Exception in thread "main" java.lang.NullPointerException
         at java.lang.Class.forName0(Native Method)
         at java.lang.Class.forName(Unknown Source)
         at Generic.QueryTest.connect(QuerryTest.java:28)
         at Generic.QueryTest.main(QuerryTest.java:35)

  • Reading a properties file from BPEL Process (jar)

    Hello,
    I've created a .jar in which i have various classes and a xml file where i keep certain properties.
    I have a BPEL Process in which i've embedded java code to access the classes in the jar.
    I've created a folder in <ORACLE_HOME>/MyLibrary and added the jar. After, i've included this path to the domain.xml located in <ORACLE_HOME>/soasuite/bpel/domains/default/config into the property: "bpelcClasspath".
    When i try to run the process, i get a "<BPELExecution::BPELProcess1> executing <exec> at line 251
    java.lang.NullPointerException
         at java.util.Properties.loadFromXML(Properties.java:700)
         at com.gepe.ldap.LDAPConfiguration.readConfig(LDAPConfiguration.java:26)".
    Apparently the properties file is not being found, am i correct?
    How can i solve this?
    Thank's in advance,
    César

    Put your class files and proerties files here:
    $ORACLE_HOME/bpel/system/classes
    marc
    http://orasoa.blogspot.com

  • Error reading Properties file

    I have a main method which i am using to call a servlet located in the same Development Component. Until recently i had the servlet location hard coded and it was working without issues. Now i decided to make the location configurable. For this i created an sap.application.global.properties file in the EAR DC which contains the WAR of this DC.
    The contents of the above file are as follows:
    SAP application properties
    SERVLET.LOCATION=http://<server-name>/ControllerServlet/servlet/com.nike.xapps.eqptsp.swem.controller.ControllerServlet
    The code i am using in the main method to call this properties file and access the property is as follows:
    Context ctx = new InitialContext();
    ApplicationConfigHandlerFactory cfgHdlr = (ApplicationConfigHandlerFactory)ctx.lookup("ApplicationConfiguration");          
    Properties props = cfgHdlr.getApplicationProperties();
    String servlet = props.getProperty("SERVLET.LOCATION");
    contained in a try...catch block.
    On dubugging i get a NoInitialContextException repeatedly in the statement where the lookup is performed.
    These are all the additions i have made for reading this properties file. Does anyone know if there is anything more that needs to be done for this to work.
    Thanks,
    Murtaza.

    thank for the help mangst. i guess the IO approach is also applicable ^^; i changed my code to:
    configFile.load( new FileInputStream( ".\\resources\\config.properties" ) );but i had a little trouble in debugging it since it starts the file search from the main project directory. however it works fine upon deployment. ^^; thanks again.
    Edited by: xnofate on Sep 23, 2008 6:21 PM

Maybe you are looking for

  • Can't open MOV files in FCP: "Searching for movie data in file..."

    I know this has been posted before, but none of them seem to match up to what I'm all of a sudden dealing with. I have 4 MOV files that I'm converting to WMV. Movie 1 & 2 exported fine. Now, however, any time I try to open another MOV file, I get an

  • G4 and Airport Express

    Our old PC laptop just died (about time.. hate Windows). So I'm trying to use an old G4 for my better half to use for her Facebook thing, and looking to connect to my cable internet service. Because of location of the G4, I cannot connect via an ethe

  • Can you copy and paste?

    Greetings. Can someone tell me if there is a way to just highlight a part of a clip in one sequence and paste it into another sequence? Normal;y what I do is use the blade tool on the part of the clip I want click on that to highlight the section, co

  • Can't shut down..

    If someone could help with this it would be much appreciated! When I try to shut down my MacBook Pro I get a window saying "The Finder cannot quit because some operations are still in progress" The operation in progress appears to be the trash window

  • E-mail sending background job diappeared in the system

    Hello Gurus, We have one e-mail sending job in our production system. The frequency of the job is daily. Now we are not able to find the sheduled/Released/Active/Finished/Canceled status for this from the last 2 days. Please help me to get back the j