Properties集合中的方法store和Properties集合中的方法load
Properties集合中的方法store
public class Demo01Properties { public static void main(String[] args) throws IOException { show02(); } private static void show02() throws IOException { // 1.一般使用"空字符串"创建Properties集合对象,添加数据 Properties prop = new Properties(); prop.setProperty("赵丽颖", "167"); prop.setProperty("迪丽热巴", "169"); prop.setProperty("古力娜扎", "170"); //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 /* FileWriter fw = new FileWriter("day09_IOAndProperties\\prop.txt"); //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 prop.store(fw,"save data"); //4.释放资源 fw.close();*/ //字节流,中文乱码 prop.store(new FileOutputStream("day09_IOAndProperties\\\\prop.txt"), ""); } }
Properties集合中的方法load
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
void load(InputStream inStream)
void load(Reader reader)
参数:
InputStream inStream:字节输入流,不能读取含有中文的键值对
Reader reader:字符输入流,能读取含有中文的键值对
使用步骤:
1.创建Properties集合对象
2.使用Properties集合对象中的方法load读取保存键值对的文件
3.遍历Properties集合
注意:
1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3.存储键值对的文件中,键与值默认都是字符串,不用再加引号
public class Demo01Properties { public static void main(String[] args) throws IOException { show03(); } /* 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用 void load(InputStream inStream) void load(Reader reader) 参数: InputStream inStream:字节输入流,不能读取含有中文的键值对 Reader reader:字符输入流,能读取含有中文的键值对 使用步骤: 1.创建Properties集合对象 2.使用Properties集合对象中的方法load读取保存键值对的文件 3.遍历Properties集合 注意: 1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号) 2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取 3.存储键值对的文件中,键与值默认都是字符串,不用再加引号 */ private static void show03() throws IOException { //1.创建Properties集合对象 Properties prop = new Properties(); //2.使用Properties集合对象中的方法load读取保存键值对的文件 prop.load(new FileReader("prop.txt")); //prop.load(new FileInputStream("prop.txt")); //3.遍历Properties集合 Set<String> set = prop.stringPropertyNames(); for (String key : set) { String value = prop.getProperty(key); System.out.println(key+"="+value); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)