2013-07-01 36 views
6

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?

Trả lời

7

Chỉ cần không mở tệp ở chế độ chắp thêm.

Bạn đọc các thuộc tính hiện có từ tệp và viết lại chúng. Nếu bạn gắn thêm vào tệp, tất cả nội dung của đối tượng Properties sẽ được nối thêm vì đây là những gì bạn yêu cầu.

Chỉ cần thay thế:

FileOutputStream fileOut = new FileOutputStream(file,true); 

với:

FileOutputStream fileOut = new FileOutputStream(file); 

Side lưu ý: bạn nên .close() dòng đầu ra của bạn trong một khối finally.

+0

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

+0

Đâ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

+0

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

1

Tôi biết điều này đã được trả lời, nhưng chỉ cho mã tham khảo sau này nên trông ít như thế này:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

class WritePropertiesFile { 

    public void WritePropertiesFile(String key, String data) { 
     FileOutputStream fileOut = null; 
     FileInputStream fileIn = null; 
     try { 
      Properties configProperty = new Properties(); 

      File file = new File("D:\\Helper.properties"); 
      fileIn = new FileInputStream(file); 
      configProperty.load(fileIn); 
      configProperty.setProperty(key, data); 
      fileOut = new FileOutputStream(file); 
      configProperty.store(fileOut, "sample properties"); 

     } catch (Exception ex) { 
      Logger.getLogger(WritePropertiesFile.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 

      try { 
       fileOut.close(); 
      } catch (IOException ex) { 
       Logger.getLogger(WritePropertiesFile.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     WritePropertiesFile help = new WritePropertiesFile(); 
     help.WritePropertiesFile("appwrite1", "write1"); 
     help.WritePropertiesFile("appwrite2", "write2"); 
     help.WritePropertiesFile("appwrite3", "write3"); 
    } 
} 
Các vấn đề liên quan