0

I have a real quick question. I have a Java program in which I use a properties file. The file is used for keeping track of the program's users. My problem is I cannot figure out how to ADD to the file. I know how to set existing properties to a value, but I don't know how to add more properties without over writing the other ones.

I would like the program to 'register' users, so to speak. Whenever a new users 'signs up', I want the program to add a new property containing the new user's information. I run into this problem though: Example:

File: numOfUsers=0

One user registers. The username is 'c00lGuy'. The program registers this in the file:

File: numOfUsers=1   user1-username=c00lGuy

Another user registers. She decides to call her username 'theGr8Girl'. The program registers this:

File: numOfUsers=2   user2-username=theGr8Girl

The file after the two users registered:

File: numOfUsers=2   user2-username=theGr8Girl

How do I prevent my program from overwriting existing lines in the file? It seems to erase the file's contents, and then add what I tell it to. I don't want it to erase the file's contents.

The code I am using to register the properties:

Properties prop = new Properties();
OutputStream output = null;

int userCount = getUserCount();
userCount++;

try {

    output = new FileOutputStream(fileName);

    // set the properties value
    prop.setProperty("numOfUsers", String.valueOf(userCount));
    prop.setProperty("user" + userCount + "-username", username);

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null)
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Rane
  • 191
  • 2
  • 13
  • 3
    That's not the purpose of a properties file. They're designed to be (read-only) configuration provided by a human. You're talking about persisting state; you should use a database (or at least, a separate file) for this. – Oliver Charlesworth Sep 27 '14 at 14:02
  • @Rane - do you have to use properties or are you just looking for a quick and easy way to persist data? – Chris Sep 27 '14 at 14:09
  • You want to store 2 user names in a properties file. But the code above uses a single username variable. You should have a collection of user names, and iterate over this collection, setting a property for each of them. – JB Nizet Sep 27 '14 at 14:10
  • @OliverCharlesworth OK. Would you mind giving me a link to the database? – Rane Sep 27 '14 at 14:12
  • @Chris I would like a way to store the information of any and every user in an external memory. I don't want it to be set to a variable in the program. – Rane Sep 27 '14 at 14:14
  • @Oliver Not necessarily true. E.g. JBoss and many app servers uses properties files for storing users. But you are of course correct, one should not use thay for production though. – Simon Fischer Sep 27 '14 at 14:14
  • @SimonFischer: I had no idea they did that! I guess all I can say is: ugh ;) – Oliver Charlesworth Sep 27 '14 at 14:16
  • @Rane: look at what your code does: it creates a new, empty file, and sets **one** property with **one** user name as value. You want **many** properties: 1 for each user name. So you obviously need a loop that iterates through all the user names and sets a property for each of them. Or you need to read the current properties file to a Properties object, add a new property to it, and rewrite the Properties object – JB Nizet Sep 27 '14 at 14:17
  • @Rane - Do you actually mean that you "don't want it to be set to a variable in the program" or do you just want it to persist when the program is not running? – Chris Sep 27 '14 at 14:19
  • Do you need to append the data each time when new user registered ? Then you should open the file with append mode and FileWriter(String fileName, boolean append) .http://stackoverflow.com/questions/4614227/how-to-add-a-new-line-of-text-to-an-existing-file-in-java – JDGuide Sep 27 '14 at 14:22

3 Answers3

0

Try something like this:

FileOutputStream out = new FileOutputStream(fileName);
props.setProperty("numOfUsers", 2);
...
props.store(out, null);
out.close();
Alexey Semenyuk
  • 3,263
  • 2
  • 32
  • 36
0

Properties files aren't really intended for this sort of usage, but if you have a small enough data set it'll work.

The step you are missing is that you need to read the properties from disk, make the changes, then save them back to disk.

Properties props = new Properties();
try{
  props.load(inputStream);
} finally {
  inputStream.close();
}
props.setProperty(....);
try{
  props.store(outputStream);
} finally {
  outputStream.close();
}

Just bear in mind that this is not at all suitable for any sort of volume processing. Also, there is a race condition if you have two threads trying to make changes to the properties file at the same time.

If you are looking for a lightweight persistent store, I highly recommend mapdb.

Kevin Day
  • 16,067
  • 8
  • 44
  • 68
0

You code is creating a new Properties object each time. Make sure to reuse the old instance when adding a user.

The typical format for a line in this file would be

user=hashedPassword

so use the username as the key and the password as a value. The number of users does not need to be stored, it is just the size of the properties map.

Simon Fischer
  • 1,154
  • 6
  • 22