Java has great API for working with properties.
In this post I am going to show example which shows how to work with properties with Properties class.
In my work I very often use properties and xml files for configuration.
Example will show how to load properties from property file,how to read them,how to write to other property file and how to set the new properties.
Properties need to put in file with properties extension.
Property file in this example is located under current working directory.
default.properties:
dirName = E:/propertiesExamplefileName = noname.properties
and code:
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class Main{ private static String dirName; private static String fileName; private static Properties properties; public static void main(String[] args) throws IOException{ properties = loadProperties("default.properties"); writeProperties(); System.out.println("Done"); } private static Properties loadProperties(String fileName) throws IOException{ Properties defaultProps = new Properties(); FileInputStream in = new FileInputStream(fileName); defaultProps.load(in); in.close(); readProperties(defaultProps); return defaultProps; } private static void readProperties(Properties defaultProperties){ dirName = defaultProperties.getProperty("dirName"); fileName = defaultProperties.getProperty("fileName"); } private static void writeProperties() throws FileNotFoundException,IOException{ Properties newProperties = new Properties(); File f = new File(dirName); f.mkdir(); File newFile = new File(f,fileName); FileOutputStream out = new FileOutputStream(newFile); setProperties(newProperties); newProperties.store(out,"My comments"); out.close(); } private static void setProperties(Properties newProperties){ newProperties.setProperty("authorName","The Developer's Info"); }}As you can see it is very easy. As usual all methods in example are simple but they show all what you need to know. If you want to know more about Properties please read API.
Download code from this post.
If you’d like to get the latest posts as soon as they’re published,subscribe to our feed!
No related posts.

Hello there! I have looked over a lot of your blog posts and noticed that they are all appear to be original and unique. Most blog owners these days do not take the time to write their own articles anymore and instead steal articles from other blogs or websites. This does not benefit anybody and as a blog owner myself I am wantin to do something about it. I asure you that this message is not spam. I have contacted many blog owers just like you and I have take the time to write each message personally. I would like you to visit my site http://www.PetPedigreeDatabase.com/ .. You will see that just like you,I take the time to write all my own articles on my blogs and am running a successful forum. The reason that I am taking your time today is because I would like your involvement in protecting yourself and your content from being taken by other blog owner that will try to reuse and recycle your content. I want to encourage you to lock your content. If you have any questions or trouble you can contact me and I will guide you in the right direction. Thank you for taking the time to read this message.