In this tutorial, we will learn how to write properties file in java
we are going to use FileOutputStream to write in file. It is going to write on classpath
Syntax
properties.store(new FileOutputStream("database.properties"), "Comments: Updated from java");
Example Create new property file
import java.io.FileOutputStream;
import java.util.Properties;
public class WriteProperties {
public static void main(String[] args) {
try {
Properties properties = new Properties();
properties.setProperty("database.url", "jdbc:mysql://localhost:3306/beginnersbug");
properties.setProperty("database.username", "root");
properties.setProperty("database.password", "password");
properties.store(new FileOutputStream("database.properties"), "Comments: Updated from java");
System.out.println("File has writen successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
File has writen successfully
Github
Related Articles
how to load property file in java