简体   繁体   中英

How do I add properties to a properties file using Java?

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();
        }

}

Try something like this:

FileOutputStream out = new FileOutputStream(fileName);
props.setProperty("numOfUsers", 2);
...
props.store(out, null);
out.close();

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM