今天就跟大家聊聊有关java中怎么利用ini4j修改ini配置文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
定义:ini文件主要由三部分构成,paramaters、section和comment组成,其中paramaters由键值对构成,用来存储数据,section是一个区块,每个区块下有所属的键值对,comment是注释,对paramaters和section进行标注和解释。
<dependency> <groupId>org.ini4j</groupId> <artifactId>ini4j</artifactId> <version>0.5.4</version></dependency>
@Data@AllArgsConstructor@NoArgsConstructorpublic class IniFileEntity { private String section; private String key; private String value;}
//我把这个写在了工具类里面(Ini4jUtils)public static boolean creatIniFile(String filePath,List<IniFileEntity> filecontent) throws IOException { File file = new File(filePath); if(file.exists()){ return false; } file.createNewFile(); Ini ini = new Ini(); ini.load(file); //将文件内容保存到ini对象中 filecontent.stream().forEach((entity)->{ ini.add(entity.getSection(),entity.getKey(),entity.getValue()== null ? "": entity.getValue()); }); //将文件内容保存到文件中 ini.store(file); return true;}// 测试@Testpublic void test(){ List<IniFileEntity> 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));}
/** * 存储文件中的内容 */@Datapublic class Ini4jFileVo { private String ip; private String ipPort; private String isUsed;}/*** 读取ini文件的内容* @param iniFile ini文件* @param fileContent ini文件中的key对应文件中的section,value对应i你文件section下的一个或多个key值* @return* @throws IOException* @throws NoSuchFieldException* @throws IllegalAccessException*/public static Ini4jFileVo readIniFile(File iniFile, Map<String,List<String>> 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)); } } /** * 这个是简略版的 * 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;}//测试@Testpublic void testReadFile(){ File file = new File("D:\\abc\\test.ini"); Map<String,List<String>> 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);}//打印结果----Ini4jFileVo(ip=1.1.1.1, ipPort=8567, isUsed=true)
/*** 修改文件内容* @param iniFile ini文件* @param updateData 更新的数据* @throws IOException*/public static void updateIniFile(File iniFile,Map<String,Map<String,String>> updateData) throws IOException { Ini ini = new Ini(); ini.load(iniFile); Section section = null; Map<String,String> 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);}@Testpublic void testUpdateFile(){ //修改 File file = new File("D:\\abc\\test.ini"); Map<String,Map<String,String>> updateData = new HashMap<>(); Map<String,String> ldap = new HashMap<>(); ldap.put("ip","8.8.8.8"); updateData.put("ldap",ldap); Ini4jUtils.updateIniFile(file,updateData); Map<String,List<String>> 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);}//测试结果----Ini4jFileVo(ip=8.8.8.8, ipPort=8567, isUsed=true)
看完上述内容,你们对java中怎么利用ini4j修改ini配置文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4607047/blog/4475135