I have a properties file that holds username and password which I use in my java program however I am not being able to store more than one username and password like I do in database and just select where username and password match the many rows in the database. I imagine I need to have a two-dimensional array stored in the file holding usernames and corresponding passwords but I’ve failed to figure out how to do it and neither has google given me a way to hold a two-dimensional array in a file yet. Here are the key/value pairs for my username and password in the file
`password=k
username=k`
And here is the code that reads them and compares with what the user inserts
`String usr = userfield.getText();
String pwd = new String(pwdfield.getPassword());
Properties config = new Properties();
InputStream is;
try {
is = new FileInputStream("config.properties");
config.load(is);
if (usr.toString().equals(config.getProperty("user").toString()) && pwd.toString().equals(config.getProperty("pass").toString())) {
new DocMenu();
lgFrame.dispose();
} else {
JOptionPane.showMessageDialog(lgFrame, "Wrong credentials try again", "Oops", JOptionPane.ERROR_MESSAGE);
}
is.close();
} catch (Exception ex) {
System.out.println(ex);
ex.printStackTrace();
}`
Could someone please tell me how to change the properties file and the code so that I am able to have multiple usernames and passwords in the file to grant access to any user as long as their username and password exist.
If you store the property name as a concatenation of some key with the username, and the password as the value like this;
..then you can check like this;