当前位置:网站首页>Properties文件的读取和写入
Properties文件的读取和写入
2022-07-21 05:17:00 【吧唧!】
什么是Properties文件
properties也是一种文本文件,是一种资源属性的文件
properties是以一种键值对(key==value)的方式进行存储内容,每一行都是一个键值对。properties类继承自Hashtable,可以用键来获取value值。
一、Properties文件的读取
我们也可以将它看成普通文件的读取,用BufferedInputStream读取,但读取出来没有形成键值对,所以没有意义。
1.properties文件的读取方式
读取properties文件,首先也需要创建BufferedInputSstream缓冲输入流传入FileInputStream作为数据源。然后创建properties对象,再调用load()方法,在load方法里传入文件流对象。(读取前需保证你所传入的数据源的properties文件在路径里是存在的)
代码示例如下:
//Properties格式文件读取
//创建输入流
try (
BufferedInputStream input = new BufferedInputStream(new FileInputStream("c:\\text\\data.properties"))) {
//创建properties文件对象
Properties pro=new Properties();
//调用load的方法,传入输入流
pro.load(input);
//打印Properties对象
System.out.println(pro);
//通过键key来获取值value
System.out.println(pro.get("A1")); //110
System.out.println(pro.get("A3")); //130
} catch (IOException e1) {
e1.printStackTrace();
}
运行结果如图:
二、properties文件的写入
1.properties文件的写入步骤简述:
创建properties对象,使用properties的put方法存入键值对,再调用properties的save或者store方法传入文件流,将键值对写入properties文件。
代码如下(示例):
try {
Properties props=new Properties();
props.put("A1", "110");
props.put("A2", "120");
props.put("A3", "130");
props.put("A4", "140");
//创建输出流,将上面键值对写入文件*.Properties文件(即data1.properties文件)
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("C:\\text\\data1.properties"));
//just do it!是给properties加的注释,
props.store(out, "just do it!");
} catch (IOException e) {
e.printStackTrace();
}
边栏推荐
猜你喜欢
OSPF comprehensive experiment
微信团队分享:微信后台在海量并发请求下是如何做到不崩溃的
The mysql57 service on the local computer stops after it is started
GRE,MGRE
阿里IM技术分享(七):闲鱼IM的在线、离线聊天数据同步机制优化实践
ospf实验(mgre)
P2P网络和虚拟专线综合实验
Teach you how to realize an efficient im long connection adaptive heartbeat keeping mechanism
搭建呼叫中心有什么方式,OKCC人工外呼适合你的企业吗?
Dx11 --- texture and lighting (flame animation, texture rotation, paste different textures)
随机推荐
Nat mode of VMware virtual machine network mode
How to use xswitch's built-in offline ASR and TTS
Why does okcc call center introduce voice into the web?
Volte HD call is VoIP
Dx11--用dx11绘制棱台,并用键盘和鼠标进行旋转缩放操作
HCIP 第一天
OSPF comprehensive experiment
OK外呼中心配置的电话系统规则
D3D函数说明
unity 粒子特效从相机外进入相机 应该是激活状态结果没有播放
What is IMS (IP multimedia subsystem)
windows mysql 5.7+ 修改表名大小写敏感属性
Connect with Tencent corporate email (authorize login free, obtain the number of unread emails)
C implementation of left leaning reactor
静态路由综合实验
呼叫中心电话客服系统搭建的种类有哪些
OSPF及MGRE综合实验
HCIP 第五天
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
阿里IM技术分享(七):闲鱼IM的在线、离线聊天数据同步机制优化实践