简体   繁体   English

从一个属性文件获取值到另一个(JAVA)

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

I have two properties files, application.properties & version.properties. 我有两个属性文件,application.properties和version.properties。 For simplicity, let's call them File1 & File2 为了简单起见,我们称它们为File1&File2

I would like to take value from File2 and set it as a value in File1. 我想从File2中获取值并将其设置为File1中的值。 For example: 例如:

File1: Property1 = Property2 File1:Property1 = Property2

File2: Property2 = VALUE 档案2: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. 我没有检查,但希望能正常工作。确保这些属性文件在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.

相关问题 如何从属性文件读取并在wsdl文件Java中获取值 - How to read from properties file and get values in wsdl file java 根据输入 - java Spring在运行时从属性文件中获取值 - Get the values from the properties file at runtime based on the input - java Spring 如何通过自定义方法从Java中的属性文件中获取值 - How to get values from properties file in Java by a custom method Java:将属性从Bean复制到另一个 - Java: Copy properties from bean to an another one 将属性从一个bean复制到另一个bean:Java - Copying Properties from one bean into another: Java 如何将值从一个Java类复制到具有相同属性的另一个类 - How to copy the values from one Java class to another class with same properties 从一个类而不是另一个类读取 Java 中的属性文件时没有这样的文件或目录 - No such file or directory when reading Properties File in Java from one Class but not another 在Java中从xml获取值到.properties文件 - Getting values from xml to .properties file in Java 按顺序从 Java 属性文件中提取值? - Pulling values from a Java Properties file in order? 如何将另一个属性文件中的值注入ValidationMessages.properties? - How to inject values from another properties file into ValidationMessages.properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM