package suanfahaonan; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class TestProtect { public static void main(String[] args) throws Exception { //创建一个属性类 Properties p=new Properties(); p.setProperty("up", "w"); p.setProperty("down", "s"); p.setProperty("left", "a"); p.setProperty("right", "d"); //将Properties中的内容存储到文件中 p.store(new FileWriter("d:/p.properties"), "very good"); } }
package suanfahaonan; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class ReadPro { public static void main(String[] args) throws Exception, IOException { Properties p=new Properties(); //从文件中读取数据,并存在P中 p.load(new FileReader("d:/p.properties")); p.remove("left");//文件里移除不了 p.setProperty("first", "we are fimalu"); p.store(new FileWriter("d:/p.properties"), "hello world"); System.out.println(p.getProperty("up")); } }