2013-11-25 49 views
0

我正在使用下面的代碼在.properties文件中編寫屬性。如何在一個.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我。怎麼可以得到的末尾添加屬性?

+0

由於您似乎正在使用'IFile',因此您應該使用'IFile.setContents'來保存文件。您當前的方法將使該文件與Eclipse不同步。 –

+0

是的,你正確的。它顯示的文件與日食不同步。 – Anu

回答

0

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

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

謝謝u.But它也附加文件的現有內容。 – Anu

0
FileOutputStream outputStream = new FileOutputStream(file1,true); 

真正告訴它的新的內容文件的末尾追加。

更多here

+0

謝謝u.But它也附加文件的現有文件內容。 – Anu

+0

@Anu檢查一下你的屬性對象中有什麼數據。我想你重新加載整個文件的內容,這就是爲什麼它追加現有的文件內容。只要確保你只有在屬性對象中需要的鍵。 – Adarsh

+0

好的,我去查。謝謝。 – Anu

1

如果您檢查屬性文件Java文檔,你可以找到它的使用哈希表來存儲鍵值對的,關鍵的順序,也沒有保證。因此,您需要使用文件操作完成後,您需要重新加載屬性文件。

相關問題