Solved

Configuration Properties

Posted on 2007-04-02
7
255 Views
Last Modified: 2008-03-10
Hi,
I have a Configuration Properties file that contains (variable, value) pairs. I would like to read those pairs from the file and use them to do database connection. Can someone show me a model

==== e.g Content of the file =======
password mypass
username myusername

============= in Addition ==========
i know how i can load the file content, but not how i pick a pair from it.

 // Read properties file.
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
    }
   
 
0
Comment
Question by:komlaaa
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 150 total points
ID: 18838702
You mean:

import java.io.*;
import java.util.*;

public class Example
{
    public static void main(String[] args)
    {
        Properties properties = new Properties();

        try
        {
            properties.load(new FileInputStream("config.txt"));
            System.out.println(properties.getProperty("username"));
            System.out.println(properties.getProperty("password"));
        }
        catch (IOException ioe)
        {
            System.err.println(ioe);
        }
    }
}

Simply use getProperty(String). Check http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html for more available methods.

Mark
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 150 total points
ID: 18839109
>>  but not how i pick a pair from it.

Use getProperty () as Mark has shown. Also close the file input stream in a finally block:

>> properties.load(new FileInputStream("filename.properties"));

FileInputStream fis = null ;
try
{
  fis = new FileInputStream ( "filename.properties" ) ;
  properties.load ( fis ) ;
} catch ( Exception e ) { e.printStackTrace () ; }
finally
{
  if ( fis != null )
    fis.close () ;
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18839117
One of the best places to look for examples is Example depot:

http://www.exampledepot.com/egs/java.util/Props.html
0
 

Author Comment

by:komlaaa
ID: 18841177
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 19033974
Yes but that's only for the environmental properties. You can use the code posted above for any general properties file.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
Securing Jmx Console and web console 2 64
jar file executable 12 37
split string containing \r\n in Java 46 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now