3 Replies Latest reply: Oct 5, 2008 2:47 PM by 807589 RSS

    Loading information from external files?

      Hi guys, I am trying to write a simple application where I can get information from external files. For example the text for a JButton or the value of a variable.

      Basically I want something where I can have a file externally with values saved in them. E.G.
      name = joe;
      hair = brown;
      and then inside my java application I could somehow get the value of "name" and "hair" for use elsewhere in my program, which would work something like this....
      System.out.println("Name: " + infoFromExternalSource.name);
      System.out.println("Hair Colour: " + infoFromExternalSource.hair);
      I was considering using XML for this but wanted to see if there was an easier way of doing it so I didn't have to use additional libraries.

      Any help would be appreciated.

      Edited by: Joe544 on 05-Oct-2008 19:01
        • 1. Re: Loading information from external files?
          You can use a java.util.Properties object:

          For example, you can have your properties stored in a file called configuration.txt, like this:
          # This is the contents of the file configuration.txt
          # This is my first property.
          age = 18
          # This is my second property
          name = richard
          # This is my third property
          buttontext = save
          To load the properties you can do the following:
          Properties properties = new Properties();          
          InputStream stream = new FileInputStream("C:/configuration.txt");
          Then, to retrieve a property called "buttontext" you do this:
          String value = properties.getProperty("buttontext");
          The getProperty method returns a String.

          Edited by: JoseLuis on Oct 5, 2008 11:36 AM
          • 2. Re: Loading information from external files?
            EDIT: Never mind, that was me not using the IDE properly. Thanks!

            The above will only work once. For example if I grab the value of "buttontext" and run the program, the output would be "Save". However, if I then go and change the value of "buttontext" to "Load" the output remains the same.

            Edited by: Joe544 on 05-Oct-2008 20:38
            • 3. Re: Loading information from external files?
              Indeed, that is the normal behavior if you read the property file once.

              Normally in your application you have the configuration file. If you modify this file, and want the new values be read you must restart the application.

              If you need that the program obtain the new values in the property file, you must read it again.

              It may be easy to program, but it's a little inconvenient in terms of performance.

              Another choice is to let the application manager to decide when to read the properties file. You can provide a button so the application manager can re-load the properties when the file has been modified.

              You should decide the best approach for your needs:
              1. Read the file once and restart the application if the values have ben changed.
              2. Read the file each time a property is required (performance bottleneck).
              3. Create a method to read the file and call it when you have modified the properties.

              Edited by: JoseLuis on Oct 5, 2008 12:45 PM