Write Properties File Java
Here you will see how to write key with value into property file using java. Below is sample java project with below folder structure:
- WriteToPropertyFile.java:
package com.javahonk; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class WriteToPropertyFile { public static void main(String[] args) { FileWriter fWriter = null; try { Properties properties = new Properties(); properties.setProperty("Name", "JavaHonk.com"); properties.setProperty("Location", "NY"); properties.setProperty("PHONE", "123456789"); fWriter = new FileWriter("resources/JavaHonk.properties"); properties.store(fWriter, "New property file"); } catch (IOException e) { e.printStackTrace(); } } }
- Once you run above program it will create and write key with value to properties file name: JavaHonk.properties inise resources folder as below:
- Output of JavaHonk.properties file:
- For more information about Java Properties API please refer oracle tutorial here