-2

I am having the following problem. I have a configuration.properties file that I would like to read inside my application. It is of the following form:

accountNames = account1, account2, account3
account1.userName = testUserName
account1.password = testUserPassword
account2.userName = secondTestUserName
account2.password = secondTestUserPassword
account2.userName = thirdTestUserName
account2.password = thirdTestUserPassword

How can I read all the accounts and store the pairs userName-userPassword in a HashMap? As I see it, I have an array of 2-dimensional arrays. I am particulary interested in a code that accesses each attribute for an account.

EDIT: I have changed my configuration.properties file to the following format:

userNames = testUserName, secondTestUserName, thirdTestUserName
testUserName = testUserPassword
secondTestUserName = secondTestUserPassword
thirdTestUserName = thirdTestUserPassword

And the code to handle this is the following:

    properties.load(new FileInputStream(configFilePath));
    for(String s : properties.getProperty("userNames").split(",")){
        clientCredentials.put(s.trim(), properties.getProperty(s.trim()));
    }

    //test:
    for(String s:clientCredentials.keySet()){
        System.out.println("Key: "+s+" & value: "+clientCredentials.get(s));
    }

Thank you for your help.

MihaiD
  • 63
  • 1
  • 10
  • You use same names for different values. It's incorrect. – Cortwave Aug 13 '14 at 10:22
  • But in the case of an account having 2 or more attributes how can I independently access them? – MihaiD Aug 13 '14 at 10:24
  • 1
    Why not pair just username & password like `firstUserName = firstUserPassword` ? – Wundwin Born Aug 13 '14 at 10:29
  • I thought of that and found an example for it but what can you do if there are more than 2 properties for each account (adding the account's email, for example)? How can you manage that situation? – MihaiD Aug 13 '14 at 10:36
  • And how do you want store it in a HashMap? It's incorrect if you want use first column as a key. – Cortwave Aug 13 '14 at 11:17
  • I think I will go for the `firstUserName = firstUserPassword` template as it seems to best suit my needs. Thank you. – MihaiD Aug 13 '14 at 11:47

1 Answers1

0

If you only care about username and password, try the following:

    final Map<String, String> accounts = new HashMap<String, String>();
    final File pwdFile = new File("path to your user/password file");
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(pwdFile));

        br.readLine();// you don't need the first line

        while (true)
        {
            String  line = br.readLine();
            if (line == null)
                break;//end of file have been reached

            final String user = line.split("=")[1].trim();

            line = br.readLine();
            if (line == null)// pwd is missing
                throw new Exception("Invalid pwd file");

            final String pwd = line.split("=")[1].trim();
            accounts.put(user, pwd);

        }

    }
    catch (final Exception e)
    {
        // add your own error handling code here
        e.printStackTrace();
    }
    finally
    {
        if (br != null)
            br.close();

    }

This code assume that line containing username will immediately be followed by a line with the password for the username of previous line.

ortis
  • 2,203
  • 2
  • 15
  • 18
  • This is a good practice in the case of regular text files but for a `configuration.properties` I was hoping for something starting with the `load(InputStream inStream)` method of the `Properties` class. – MihaiD Aug 13 '14 at 10:47
  • Because you want it in a `HashMap`, it is easier and faster to use this code. Otherwise, you will have to load your file with using `Properties.load` then iterate other each elements of the `Properties` object. A second advantage it is that you can apply some custom formatting while loading the file (trim, uppercase, ...) – ortis Aug 13 '14 at 10:59