当前位置:网站首页>properties文件的读取和写入
properties文件的读取和写入
2022-08-09 15:01:00 【鸣筝鸣筝】
目录
properties文件是一种属性文件,这种文件以key=value(键值对)格式存储内容。Java中可以使用Properties类来读取这个文件,使用Properties类中的getProperties(key)方法来得到对应的数据。一般properties文件作为一些参数的存储,使得代码更加灵活。
一、properties文件的读取
(1)使用BufferedInputStream(缓冲输入流)创建输入流进行读取,读取后以key=value(键值对)格式存储数据。
代码实现:
//Properties格式文件的读取
//创建输入流
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\student\\aaa.properties"))) {
int data =-1;
while((data=bis.read())!=-1) {
System.out.print((char)data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//运行结果
//k=12
//b=34
//c=11(2)使用Properties类中的load()方法读取,将“输入流”加载到Properties集合对象中,根据key来获取value的值
代码实现:
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\student\\aaa.properties"))) {
Properties props=new Properties();
props.load(bis);//将“输入流”加载到Properties集合对象中
//根据key,获取value
System.out.println(props.get("k"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//运行结果
//12二、properties文件的写入
因为properties文件数据是以集合形式存储,所以可以用put()方法将KV键值对存入Properties集合中,再通过输出流使用store()方法将集合中的临时数据,持久化写入硬盘中存储。
//properties格式文件的写入
try{
Properties props=new Properties();
props.put("Q", "123");
props.put("W", "432");
props.put("E", "986");
props.put("R", "457");
//使用“输出流”,将properties集合中的KV键值对,写入*.properties文件
try (BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("D:\\student\\bbb.properties"))) {
props.store(bos, "just do IT");
}
} catch (IOException e) {
e.printStackTrace();
}
//运行结果
// #just do IT
// #Wed Jun 01 00:14:57 CST 2022
// W=432
// R=457
// Q=123
// E=986边栏推荐
- 输入不定长数组,输入一个字符串,既包含字符,又包含数字,输出数组,输入一个二维数组,字符和数字都可
- 【力扣】617. 合并二叉树
- Unity UI框架思路与实现
- Gray Relevance Matrix——Application of MATLAB in Mathematical Modeling
- 数据库导入导出sql数据库文件
- 【力扣】98. 验证二叉搜索树
- TOPSIS优劣解距离法
- SVM支持向量机——MATLAB在数学建模中的应用
- #define DEBUG(format, ...) 以及 #、##、__VA_ARGS__和##__VA_ARGS__的作用
- Analytic Hierarchy Process (AHP) - Applications of MATLAB in Mathematical Modeling (2nd Edition)
猜你喜欢
随机推荐
堆(heap)系列_0x0A:3种方法一次性解决堆溢出问题
unity shader 入门 全透明与半透明效果实现
【SQL】175. 组合两个表
ASCII码表
【力扣】114. 二叉树展开为链表
Vim practical skills_3. Visual mode and command mode
输入不定长数组,输入一个字符串,既包含字符,又包含数字,输出数组,输入一个二维数组,字符和数字都可
Basic Concepts of Software Security
相关性分析
Principal Component Analysis - Applications of MATLAB in Mathematical Modeling (2nd Edition)
[Paper reading] LIME: Low-light Image Enhancement via Illumination Map Estimation (the most complete notes)
【QT】窗口几何布局学习
TCP/IP协议组——完整工作过程分析
杭州富阳科目三考试
主成分分析——MATLAB在数学建模中的应用(第2版)
堆(heap)系列_0x02:堆的前世今生(WinDbg+Visual Studio汇编)
软件安全内存区域详解
Vim practical skills_0.vim - introduction
Gray Relevance Matrix——Application of MATLAB in Mathematical Modeling
配置 vscode 让它变得更好用




![writeUP-[第五空间2019 决赛]PWN5(待进一步完善待研究内容)](/img/3c/ff6f1dd7402b2ccc442e806e407710.png)



