You can create a new property file and write into it or you can write in an existing property file.
If you are creating new property file, below code will create the new property file "write.properties" and write the given keys and values into it.
Let's create "write.properties" and add below keys and values :
#This is header text.
#Wed Jun 06 22:11:58 CDT 2018
name=David // name is KEY and David is VALUE
class=Middle
direction=North
public class Practice {
public static void write2Prop() {
try {
String filePath = System.getProperty("user.dir")+"\\src\\practice\\write.properties";
File file = new File(filePath);
FileOutputStream fOS = new FileOutputStream(file);
Properties prop = new Properties();
prop.setProperty("name", "David");
prop.setProperty("class", "Middle");
prop.setProperty("direction", "North");
prop.store(fOS, "The is header text.");
fOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Now, let's call above method in main method:
public static void main(String[] args) {
write2Prop();
}
}
When you run this code, it will create the property file "write.properties" and write the KEYS and VALUES into it.
If the file is already existing and you are writing into the existing property file then the contents of the property file will be replaced with the new KEYS and VALUES.
To learn , how to read property file , go to Reading Property File
No comments:
Post a Comment