Tôi đang sử dụng sau mã cho ghi dữ liệu vào thuộc tính tập tinLàm cách nào để nối dữ liệu mới vào dữ liệu hiện có trong tệp thuộc tính?
public void WritePropertiesFile(String key, String data)
{
Properties configProperty = new Properties();
configProperty.setProperty(key, data);
File file = new File("D:\\Helper.properties");
FileOutputStream fileOut = new FileOutputStream(file,true);
configProperty.store(fileOut, "sample properties");
fileOut.close();
}
I am calling the above method 3 times as follows:
help.WritePropertiesFile("appwrite1","write1");
help.WritePropertiesFile("appwrite2","write2");
help.WritePropertiesFile("appwrite3","write3");
Tuy nhiên, dữ liệu trong tập tin Helper.properties được hiển thị như sau:
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite2=write2
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite3=write3
appwrite2=write2
appwrite1=write1
Tôi muốn dữ liệu gắn liền với các dữ liệu hiện có và không muốn dữ liệu trùng lặp, tức là như sau:
appwrite3=write3
appwrite2=write2
appwrite1=write1
Vui lòng đề xuất cách thực hiện?
Hi FGE, cảm ơn vì câu trả lời ... tôi đã cố gắng mã bạn đã đề cập, đó là làm việc tốt. Nhưng tôi đã có một vấn đề khi tôi đang truy cập các phương pháp từ tập tin lớp khác. Tôi đang gọi hàm trên từ một tệp lớp như sau: help.WritePropertiesFile ("appwrite1", "write1"); help.WritePropertiesFile ("appwrite2", "write2"); help.WritePropertiesFile ("appwrite3", "write3"); – Vikas
Đây là một vấn đề khác hoàn toàn;) Bạn không hiển thị đủ mã để được trợ giúp về điều đó. – fge
từ tệp lớp khác tôi đang gọi như sau: help.WritePropertiesFile ("appwrite4", "write4"); help.WritePropertiesFile ("appwrite5", "write5"); help.WritePropertiesFile ("appwrite6", "write6"); đầu ra được hiển thị là appwrite4 = write4 appwrite5 = write5 appwrite6 = write6 Xóa dữ liệu được viết từ class1 và thay thế bằng class2 ... _Xin vui lòng cho tôi biết cách lưu trữ dữ liệu được viết từ cả hai tệp lớp ? – Vikas