2013-07-01 9 views
6

मैं प्रॉपर्टी को डेटा लिखने के लिए कोड निम्न का उपयोग कर रहा फ़ाइलगुण फ़ाइल में मौजूदा डेटा में नया डेटा कैसे जोड़ना है?

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"); 

हालांकि, Helper.properties फ़ाइल में डेटा इस प्रकार प्रदर्शित होता है:

#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 

मैं मौजूदा डेटा को संलग्न करने के लिए डेटा चाहते हैं और डुप्लिकेट डेटा नहीं चाहता है, जो निम्नानुसार है:

appwrite3=write3 
appwrite2=write2 
appwrite1=write1 

कृपया सुझाव दें कि यह कैसे करें?

उत्तर

7

बस संलग्न मोड में फ़ाइल को न खोलें।

आप फ़ाइल से मौजूदा गुण पढ़ते हैं और उन्हें फिर से लिखते हैं। यदि आप फ़ाइल में शामिल होते हैं, तो Properties ऑब्जेक्ट की सभी सामग्री संलग्न की जाएगी क्योंकि आपने यही पूछा है।

बस की जगह:

FileOutputStream fileOut = new FileOutputStream(file,true); 

साथ:

FileOutputStream fileOut = new FileOutputStream(file); 

साइड नोट: यदि आप एक finally ब्लॉक में अपने उत्पादन धारा .close() चाहिए।

+0

हाय fge, धन्यवाद जवाब के लिए ... मैं कोड आप उल्लेख किया है की कोशिश की, यह है अच्छा कर रहा है। लेकिन मुझे एक समस्या मिली जब मैं अन्य वर्ग फ़ाइल से विधि का उपयोग कर रहा हूं। मैं उपरोक्त फ़ंक्शन को एक वर्ग फ़ाइल से निम्नानुसार कॉल कर रहा हूं: help.WritePropertiesFile ("appwrite1", "write1"); help.WritePropertiesFile ("appwrite2", "write2"); help.WritePropertiesFile ("appwrite3", "write3"); – Vikas

+0

यह एक और समस्या पूरी तरह से है;) आप इसके साथ मदद के लिए पर्याप्त कोड नहीं दिखाते हैं। अन्य कक्षा फ़ाइल से – fge

+0

मैं निम्नानुसार कॉल कर रहा हूं: help.WritePropertiesFile ("appwrite4", "write4"); help.WritePropertiesFile ("appwrite5", "write5"); help.WritePropertiesFile ("appwrite6", "write6"); आउटपुट के रूप में appwrite4 = write4 appwrite5 = write5 appwrite6 = write6 यह डेटा Class1 से लिखा दूर करने और class2 इसकी जगह दिखाई देती है ... _Please मुझे पता डेटा दोनों वर्ग फ़ाइलों से लिखा स्टोर करने के लिए कैसे करते हैं ? – Vikas

1

मैं इस उत्तर दिया गया है, लेकिन सिर्फ भविष्य में संदर्भ के कोड के लिए दिखना चाहिए पता अधिक कम इस तरह:

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"); 
    } 
} 
संबंधित मुद्दे