logo

How to create a properties file in Java

How to create a properties file in Java.

Let's create a properties file. Create instance of class Properties.

Properties properties = new Properties();

 

Add properties you want.

properties.setProperty("project-name", "JavaVids"); 

 

Create a new writer.

FileWriter writer = new FileWriter("conf.properties"); 

 

Store properties to writer. You can also add a comment.

properties.store(writer, "Jiri Pinkas"); 

 

Close writer.

writer.close(); 

 

Add throws declaration to ignore exceptions. This is how you do it quick and dirty. To handle exceptions properly change code to this:

Properties properties = new Properties();
properties.setProperty("project-name", "JavaVids");
FileWriter writer = null;
try {
	writer = new FileWriter("conf.properties");
	properties.store(writer, "Jiri Pinkas");
} catch (IOException ex) {
	ex.printStackTrace();
} finally {
	if (writer != null) {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search