[英]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.