Write Properties File Java

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:

Write Properties File Java

  • 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:

Write Properties File Java

  • Output of JavaHonk.properties file:

Write Properties File Java

  • For more information about Java Properties API please refer oracle tutorial here 

Leave a Reply

Your email address will not be published. Required fields are marked *