当前位置:网站首页>Reading and writing properties file
Reading and writing properties file
2022-07-21 23:18:00 【Bravo!】
List of articles
What is? Properties file
properties It is also a text file , Is a resource attribute file
properties Is a key value pair (key==value) To store content , Each line is a key value pair .properties Class inherits from Hashtable, You can use the key to get value value .
One 、Properties File reading
We can also regard it as the reading of ordinary files , use BufferedInputStream Read , But the key value pair is not formed after reading , So it doesn't make sense .
1.properties How the file is read
Read properties file , First, you also need to create BufferedInputSstream Buffer input stream incoming FileInputStream As a data source . Then create properties object , Call again load() Method , stay load Method into the file stream object .( Before reading, you need to ensure that the data source you have passed in properties The file exists in the path )
The code example is as follows :
//Properties Format file read
// Create input stream
try (
BufferedInputStream input = new BufferedInputStream(new FileInputStream("c:\\text\\data.properties"))) {
// establish properties File object
Properties pro=new Properties();
// call load Methods , Incoming input stream
pro.load(input);
// Print Properties object
System.out.println(pro);
// Pass key key To get the value value
System.out.println(pro.get("A1")); //110
System.out.println(pro.get("A3")); //130
} catch (IOException e1) {
e1.printStackTrace();
}
The operation result is as shown in the figure :
Two 、properties Writing files
1.properties Brief description of the writing steps of the file :
establish properties object , Use properties Of put Method to store key value pairs , Call again properties Of save perhaps store Method to pass in the file stream , Write key value pairs to properties file .
The code is as follows ( Example ):
try {
Properties props=new Properties();
props.put("A1", "110");
props.put("A2", "120");
props.put("A3", "130");
props.put("A4", "140");
// Create output stream , Write the above key value pair to the file *.Properties file ( namely data1.properties file )
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("C:\\text\\data1.properties"));
//just do it! It's for properties Added note ,
props.store(out, "just do it!");
} catch (IOException e) {
e.printStackTrace();
}
边栏推荐
- Irregular area of OSPF
- Hcip day 7
- OSPF working process and its simple experiment
- HCIP day1
- Single arm routing configuration
- Comprehensive experiment of static routing
- 栈模拟队列
- Fiddler actual combat - small white level - installation and basic packet capturing and packet changing operations
- HCIA_ Nat experiment
- A brief summary of the programming journey in recent years···
猜你喜欢
随机推荐
"= =" difference from equals
【无标题】HCIP第一天笔记
Hcip rip the next day
HCIP:ospf第一节
Mongodb replaces some contents of a field
字符串的常见方法总结
Simple configuration of log4j
HCIP第九天
Hcip day 9
OSPF实验
函数的参数
第三天 网络类型
Hcip day 8
Zip文件的读取与写入
Properties文件的读取和写入
OSPF routing control
Comprehensive experiment of static routing
OSPF experiment in mGRE environment
Excel文件的解析
OSI seven layer reference model