Import Java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.IOException; Import Java.util.hashmap;import java.util.map;import java.util.properties;import java.util.Set; Import Org.junit.Test; public class Propertiestest {public static void main (string[] args) {}/** * Delete key in configuration file * @throws IO Exception * */@Test public void Delete () throws ioexception{Properties Properties = New Properties () ; File File = new file ("Test.properties"); FileInputStream fis = new FileInputStream (file); Properties.load (FIS); Must first use MAP to save all the content first, or delete, the original content has not map<string, string> map = new hashmap<string, string> (); set<object> KeySet = Properties.keyset (); System.out.println (Keyset.size ()); For (object Object:keyset) {string key = (string) Object; String value = (string) properties.get (key); System.out.println (key+ "=" +value); Map.put (key, value); }//Delete key Map.Remove ("Age3"); Properties.remove ("Age3"); For (java.util.map.entry<string, string> entry:map.entrySet ()) {Properties.setproperty (Entry.getkey (), E Ntry.getvalue ()); } fileoutputstream fos = new FileOutputStream (file); Properties.store (Fos, "delete key:age3 in config file"); Fos.close (); Fis.close (); }/** * Changes to configuration file * @throws IOException */@Test public void Update () throws ioexception{ Properties Properties = new properties (); File File = new file ("Test.properties"); FileInputStream fis = new FileInputStream (file); Properties.load (FIS); Must first use MAP to save all the content first, or an update, the original content has not map<string, string> Map = new hashmap<string, string> (); set<object> KeySet = Properties.keyset (); System.out.println (Keyset.size ()); For (object Object:keyset) {string key = (string) Object; String value = (string) properties.get (key); System.out.println (key+ "=" +value); Map.put (key, value); } map.put ("Name", "John Doe"); Map.put ("Age", "222"); Map.put ("Age3", "222"); For (java.util.map.entry<string, string> entry:map.entrySet ()) {Properties.setproperty (Entry.getkey (), E Ntry.getvalue ()); } fileoutputstream fos = new FileOutputStream (file); Properties.store (FOS, "update config file"); Fos.close (); Fis.close (); /** * Write content to config file * @throws IOException */@Test public void Write () throws IOException { Properties Properties = new properties (); String file = "Aa.properties"; FileOutputStream fos = new FileOutputStream (file); Properties.setproperty ("AA", "123"); Properties.setproperty ("BB", "456"); Properties.setproperty ("CC", "789"); Properties.setproperty ("name", "Zhang San"); Properties.store (FOS, "Save File"); Fos.close (); }/** * Read config file * @throws IOException */@Test public void Read () throws ioexception{Prop Erties Properties = new properties (); File File = new file ("Test.properties"); FileInputStream fis = new FileInputStream (file); Properties.load (FIS); set<object> KeySet = Properties.keyset (); for (Object object:keyset) {System.out.println (object.tostring () + "=" +properties.getproperty (object.tostring () )); } } }
|