简体   繁体   English

如何使用Java将属性添加到属性文件?

[英]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. 我有一个Java程序,其中使用了属性文件。 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'. 用户名是“ c00lGuy”。 The program registers this in the file: 程序将其注册到文件中:

File: numOfUsers=1   user1-username=c00lGuy

Another user registers. 另一个用户注册。 She decides to call her username 'theGr8Girl'. 她决定将其用户名称为“ 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 . 如果您正在寻找轻量级的持久性存储,我强烈建议mapdb

You code is creating a new Properties object each time. 您的代码每次都创建一个新的Properties对象。 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. 用户数量不需要存储,仅是属性映射的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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