In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use ini4j to modify the ini configuration file in java, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Definition: the ini file is mainly composed of three parts, paramaters, section and comment, in which paramaters is composed of key-value pairs, which is used to store data, section is a block, each block has its own key-value pair, and comment is a comment, marking and interpreting paramaters and section.
Using the utility class ini4j to introduce dependent jar packages
Org.ini4j
Ini4j
0.5.4
Create an instance object of the .ini file (optional, I do this for testing convenience) @ Data
@ AllArgsConstructor
@ NoArgsConstructor
Public class IniFileEntity {
Private String section
Private String key
Private String value
}
Create the .ini file / / I wrote this in the utility class (Ini4jUtils)
Public static boolean creatIniFile (String filePath,List filecontent) throws IOException {
File file = new File (filePath)
If (file.exists ()) {
Return false
}
File.createNewFile ()
Ini ini = new Ini ()
Ini.load (file)
/ / Save the file contents to the ini object
Filecontent.stream () .forEach ((entity)-> {
Ini.add (entity.getSection (), entity.getKey (), entity.getValue () = = null? ": entity.getValue ())
});
/ / Save the contents of the file to the file
Ini.store (file)
Return true
}
/ / Test
@ Test
Public void test () {
List list = Arrays.asList (new IniFileEntity ("ldap", "ip", "1.1.1.1")
New IniFileEntity ("ldap", "ipPort", "8567")
New IniFileEntity ("test", "isUsed", "true"))
System.out.println (Ini4jUtils.creatIniFile ("D:\ abc\\ test.ini", list))
}
Insert a picture description here to read the .ini file / * *
* store the contents of the file
, /
@ Data
Public class Ini4jFileVo {
Private String ip
Private String ipPort
Private String isUsed
}
/ * *
* read the contents of the ini file
* @ param iniFile ini file
The key in the * @ param fileContent ini file corresponds to the section,value in the file corresponding to one or more key values under the section of your file.
* @ return
* @ throws IOException
* @ throws NoSuchFieldException
* @ throws IllegalAccessException
, /
Public static Ini4jFileVo readIniFile (File iniFile, Map fileContent) throws IOException, NoSuchFieldException, IllegalAccessException {
Ini4jFileVo fileVo = new Ini4jFileVo ()
Ini ini = new Ini ()
Ini.load (iniFile)
Section section = null
Field field = null
For (String key: fileContent.keySet ()) {
Section = ini.get (key)
For (String value: fileContent.get (key)) {
Field = fileVo.getClass () .getDeclaredField (value)
Field.setAccessible (true)
Field.set (fileVo, section.get (value))
}
}
/ * *
* this is an abbreviated version
* Section section = ini.get ("ldap")
* fileVo.setIp (section.get ("ip"))
* fileVo.setIpPort (section.get ("port"))
*
* section = ini.get ("test")
* fileVo.setIsUsed (section.get ("isUsed"))
, /
Return fileVo
}
/ / Test
@ Test
Public void testReadFile () {
File file = new File ("D:\\ abc\\ test.ini")
Map fileContent = new HashMap ()
FileContent.put ("ldap", Arrays.asList ("ip", "ipPort"))
FileContent.put ("test", Arrays.asList ("isUsed"))
Ini4jFileVo fileVo = Ini4jUtils.readIniFile (file,fileContent)
System.out.println (fileVo)
}
/ / print the result-Ini4jFileVo (ip=1.1.1.1, ipPort=8567, isUsed=true)
Modify .ini file / * *
* modify the contents of the file
* @ param iniFile ini file
* @ param updateData updated data
* @ throws IOException
, /
Public static void updateIniFile (File iniFile,Map updateData) throws IOException {
Ini ini = new Ini ()
Ini.load (iniFile)
Section section = null
Map dataMap = null
For (String sect: updateData.keySet ()) {
Section = ini.get (sect)
DataMap = updateData.get (sect)
For (String key: dataMap.keySet ()) {
Section.put (key,dataMap.get (key) = = null? ":
DataMap.get (key))
}
}
Ini.store (iniFile)
}
@ Test
Public void testUpdateFile () {
/ / modify
File file = new File ("D:\\ abc\\ test.ini")
Map updateData = new HashMap ()
Map ldap = new
HashMap ()
Ldap.put ("ip", "8.8.8.8")
UpdateData.put ("ldap", ldap)
Ini4jUtils.updateIniFile (file,updateData)
Map fileContent = new HashMap ()
FileContent.put ("ldap", Arrays.asList ("ip", "ipPort"))
FileContent.put ("test", Arrays.asList ("isUsed"))
Ini4jFileVo fileVo = Ini4jUtils.readIniFile (file,fileContent)
System.out.println (fileVo)
}
/ / Test results-Ini4jFileVo (ip=8.8.8.8, ipPort=8567, isUsed=true)
After reading the above, do you have any further understanding of how to use ini4j to modify the ini configuration file in java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.