当前位置:网站首页>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();
}
}
}
边栏推荐
- Use js to count the number of occurrences of each string in the string array, and format it into an object array.
- JWT快速入门
- Web page basic label
- Jmeter性能测试之命令行执行和生成测试报告
- Brief description of hystrix configuration
- AQS implementation principle
- Libevent custom event (asynchronous)
- 深度学习模型部署
- PyTorch
- JMeter performance test saves the results of each interface request to a file
猜你喜欢
随机推荐
Next item recommendations in short sessions
音视频学习(十)——ps流
ShardingSphere数据分片
Use of JMeter performance test to store response content to file listener
Establishment and use of openstack cloud platform
Command line execution and test report generation of JMeter performance test
如何关闭高位端口
在线问题反馈模块实战(十四):实现在线答疑功能
Shardingjdbc pit record
C # use log4net to record logs (basic chapter)
Hystrix配置简单说明
The analysis, solution and development of the problem of router dropping frequently
[classic thesis of recommendation system (10)] Alibaba SDM model
Learning Efficient Convolutional Networks Through Network Slimming
爬虫->TpImgspider
Tensorflow learning diary tflearn
Shardingsphere data slicing
Regression analysis code implementation
C语言关键字extern
Idea shortcut key








