簡體   English   中英

如何在一個.properties文件的末尾添加屬性

[英]How to add property at the end in one .properties file

我正在使用下面的代碼在.properties文件中寫入屬性。

InputStream inputStream = null;
    try {
        inputStream = file.getContents();
        Properties properties = new Properties();
        properties.load(inputStream);
        inputStream.close();
        properties.setProperty(propertyKey.trim(), propertyValue.trim());
        File file1 = file.getRawLocation().makeAbsolute().toFile();
        FileOutputStream outputStream = new FileOutputStream(file1);
        properties.store(outputStream, null);
        outputStream.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (CoreException ex) {
        ex.printStackTrace();
    }

雖然它是在排序后的位置添加屬性,但不是在末尾添加。但是我希望它總是at the end添加。我怎么能得到它?

如果檢查屬性文件java doc,則可以使用HashTable查找其存儲鍵值對,也不保證鍵的順序。 因此,一旦完成操作,您需要使用文件操作,您需要重新加載屬性文件。

使用FileWriter附加到文件末尾(構造函數的第二個參數定義附加模式):

try (FileWriter out = new FileWriter(file, true)) {
    out.append(property + "=" + value);
}
FileOutputStream outputStream = new FileOutputStream(file1,true);

true表示將新內容附加到文件末尾。

在這里閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM