簡體   English   中英

從一個屬性文件獲取值到另一個(JAVA)

[英]Get values from one properties file into another (JAVA)

我有兩個屬性文件,application.properties和version.properties。 為了簡單起見,我們稱它們為File1&File2

我想從File2中獲取值並將其設置為File1中的值。 例如:

File1:Property1 = Property2

檔案2:Property2 = VALUE

我不確定使用什么腳本或如何使用它,因為使用屬性對我來說是新的。

感謝幫助。

我沒有檢查,但希望能正常工作。確保這些屬性文件在jar之外。

File file1 = new File("application.properties");//change path to outside//document\..\
File file2 = new File("version.properties");

try {
    FileReader reader = new FileReader(file2);
    Properties props = new Properties();
    props.load(reader);
    String prop2 = props.getProperty("Property2");
    reader.close();

    Properties props2 = new Properties();
    FileOutputStream fos = new FileOutputStream(file1);

    props2.setProperty("Property2", prop2);
    //writing properites into properties file from Java
    props2.store(fos, "wrote");
    fos.close();


} catch (FileNotFoundException ex) {
    // file does not exist
} catch (IOException ex) {
    // I/O error
}

暫無
暫無

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

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