简体   繁体   中英

Get values from one properties file into another (JAVA)

I have two properties files, application.properties & version.properties. For simplicity, let's call them File1 & File2

I would like to take value from File2 and set it as a value in File1. For example:

File1: Property1 = Property2

File2: Property2 = VALUE

I'm not sure what script or how to use it since using properties is new for me.

Thanks for help.

i didn't check but hope this will work.make sure those property files exist outside of 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM