How to write to properties file in Java
One of most visited posts on this blog is How to read properties file in Java. In that post I explained how you can read from a properties file. But many people came to that post searching for an example on how to write to a properties file. So I thought it will be beneficial for those visitors if we a have separate post with an example of how to write to a properties file in Java.
import java.util.*; import java.io.*; class WriteToPropertiesFile { PrintWriter output = null; public void writeToFile(HashMap map,String fileName) throws Exception{ Properties properties = new Properties(); Set set = map.keySet(); Iterator itr = set.iterator(); while(itr.hasNext()){ String key = (String)itr.next(); String value = map.get(key); properties.setProperty(key, value); } //We have all the properties, now write them to the file. //The second argument is optional. You can use it to identify the file. properties.store(new FileOutputStream(fileName),"Java properties test"); //To keep this example simple, I did not include any exception handling //code, but in your application you might want to handle exceptions here //according to your requirements. } }
This method takes a HashMap of keys and values and the name of the properties file to be created as the input parameters. The idea behind using the hash map is to make this method generic. You can get your properties from an input file, command line arguments or any where else. But once you have those values you can put them in a HashMap and call this method to write those properties to a file.
Here is simple method that demonstrates how you can call the above method to write values to a properties file.
public static void main(String args[]) throws Exception{ WriteToPropertiesFile wtp = new WriteToPropertiesFile(); HashMap map = new HashMap(); map.put("lang","java"); map.put("OS","Windows"); map.put("version","1.6"); wtp.writeToFile(map,"C://temp//test//sample.properties"); }
Here is the output.
#Java properties test #Mon Nov 09 12:41:34 CST 2009 version=1.6 OS=Windows lang=java
Good Luck!
emerson
November 17, 2009 @ 12:39 am
Hey guys! Did you sweat excessively? Here is the good news for you to solve that sweating problems! My friend told me that this one would work for me and so I tried it. After several uses, i observed that it is really effective! No more excess sweat. You should check it out.
April
November 17, 2009 @ 11:42 am
Wow…. you have such great ideas on Java. It looks very complicated but the end result is unexplainably terrific. I think I’m going to take on a computer course so I can also give great advice to other people and how to maximize the use of their PC. Thanks for the helpful info.
vishwajeet
June 14, 2010 @ 3:54 am
one doesn’t need to iterate over Map and insert its content
into Properties one at a time like u have done in following
way,
# Set set = map.keySet();
# Iterator itr = set.iterator();
# while(itr.hasNext()){
# String key = (String)itr.next();
# String value = map.get(key);
# properties.setProperty(key, value);
# }
Instead one can replace above 7 lines of code with a
single line as demonstrated below:
properties.putAll(map)
Alper
December 10, 2011 @ 2:52 am
nice advice , thanks