package J20250728; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; /** * @author WanJl * @version 1.0 * @title Demo06_Properties * @description 将集合中的元素存入到文件 * @create 2025/7/28 */ public class Demo06_Properties { public static void main(String[] args) throws IOException { Properties prop=new Properties(); //使用Properties特有的方法添加属性键值对(添加元素) prop.setProperty("name","张三"); prop.setProperty("age","25"); prop.setProperty("sex","男"); prop.setProperty("username","zhangsan"); prop.setProperty("password","123456"); //创建字符输出流对象 //myfile.properties是一个文件 //.properties作为后缀名,是一种专门存储属性的文件格式 FileWriter fw=new FileWriter("myfile.properties"); //将prop集合中的键值对写入到指定文件 prop.store(fw,"这是一个非常有意思的介绍"); //关闭流 fw.close(); //读取数据 } }