当前位置:网站首页>Reading and writing properties file
Reading and writing properties file
2022-07-26 07:54:00 【I like iced black tea】
What is? properties file ?
properties File is a property file , This kind of document is in the form of key=value Formats store content .Java Can be used in Properties Class to read this file , Use Propertie Class getProperty(key) Method can get the corresponding data . commonly properties Files are stored as some parameters , Make the code more flexible .
Properties File reading :
Normal read : utilize BufferInputStream Buffer the input stream for reading , You can read all the contents of the file , But such reading is lost properties The characteristics of the document . This document is based on key=value Formats store content .
Code implementation :
public class Main {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:\\IO flow \\data.properties"))) {
int data = -1;
while((data = bis.read())!=-1) {
System.out.print((char)data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} Use Properties Class read : We need to use load() Methods will “ Input stream ” Load to Properties In the collection object . So that we can base on key To get value Value .
Code implementation :
public class Main {
public static void main(String[] args) {
try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\IO flow \\data.properties"))) {
Properties props = new Properties();
props.load(bis); // take “ Input stream ” Load to Properties In the collection object
// according to key, obtain value
System.out.println(props.get("cn"));
System.out.println(props.get("kr"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Properties Writing files :
First you need to create a Properties object props, Use put() Method to put keys and values into Properties In the collection object , Then you need to use the output stream to pass store() Method to put the temporary data in the set , Persistent write to hard disk for storage .
Code implementation :
public class Main {
public static void main(String[] args) {
//Properties Writing of format file
Properties props = new Properties();
props.put("f1", "2222");
props.put("f2", "2342");
// Use output stream , take Properties In the collection KV Key value pair , write in .properties file
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\IO flow \\demo.properties"))) {
props.store(bos, "just do it");
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
- FTP service
- The analysis, solution and development of the problem of router dropping frequently
- Utils connection pool
- 2022.7.22DAY612
- Wrong Addition
- 在线问题反馈模块实战(十四):实现在线答疑功能
- Meta universe infrastructure: analysis of the advantages of Web 3.0 chain33
- Ethernet switching security
- Regression analysis code implementation
- 爬虫->TpImgspider
猜你喜欢

WCF deployed on IIS

How to determine the authenticity of the website you visit -- certificate system

Network trimming: a data driven neuron pruning approach towards efficient deep architectures paper translation / Notes

Wrong Addition

Establishment and use of openstack cloud platform
![[uniapp] encapsulation of multiple payment methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[uniapp] encapsulation of multiple payment methods

元宇宙基础设施:WEB 3.0 chain33 优势分析

Speech at 2021 global machine learning conference

Network ()

Command line execution and test report generation of JMeter performance test
随机推荐
微服务feign调用时候,token丢失问题解决方案
Interview question set
JMeter performance test saves the results of each interface request to a file
Pycharm common shortcut keys
时间序列分析预测实战之ARIMA模型
utils 连接池
2022.7.22DAY612
Practice of online question feedback module (XIV): realize online question answering function
Open source management system based on ThinkPHP
Summary of distributed related interview questions
给项目日志加上traceid
The bigger the project is, the bigger it is. This is how I split it
Idea shortcut key
API (common class 2)
"Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
JMeter性能测试之使用CSV文件参数化
MMOE multi-objective modeling
1.MySQL架构篇【mysql高级】
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
Regular expression rules and common regular expressions