对*.ini文件的增删改查
Posted chengxuzhijia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对*.ini文件的增删改查相关的知识,希望对你有一定的参考价值。
文件的格式有很多种 *.ini 一般是带一个齿轮的配置文件,这个文件的操作有专门的类库进行处理不行自己硬编码 ini4j.jar
这个jar包专门封装了处理*.ini文件的方法。
1 public interface IParaManager { 2 public int getKey(String section, String key, CInt value); 3 public int getkey(String section, String key, CString value); 4 public int getkey(String section, String key, CDouble value); 5 public Map<String, String> getGeys(String section); 6 public int setKey(String section, String key, String value); 7 8 }
1 public class CFileParaManagerNew implements IParaManager { 2 3 private Logger logger = Logger.getLogger(CFileParaManager.class); 4 Wini ini = null; 5 public CFileParaManagerNew(String fileName) throws Exception{ 6 String path = System.getProperty("user.dir");//参数待定 7 String filePath = path + File.separator +"resources" +File.separator + fileName; 8 logger.debug("filePath is" + filePath); 9 Config config = new Config(); 10 config.setMultiSection(true); 11 ini = new Wini(new File(filePath)); 12 } 13 @Override 14 public int getKey(String section, String key, CInt value) { 15 logger.debug("CFileParaManager getKey CInt start"); 16 String str = Integer.valueOf(value.get()).toString(); 17 CString cString = new CString(); 18 cString.set(str); 19 int num = getkey(section, key, cString); 20 if (num != 0) { 21 logger.debug("CFileParaManager getKey CInt fail"); 22 return -1; 23 } 24 int val = Integer.parseInt(cString.get()); 25 value.set(val); 26 logger.debug("CFileParaManager getKey CInt end"); 27 return 0; 28 } 29 30 @Override 31 public int getkey(String section, String key, CString value) { 32 logger.debug("CFileParaManager getKey CString start"); 33 if (null == section || null == key) { 34 logger.debug("CFileParaManager getKey CString start section or key or value is null"); 35 return -1; 36 } 37 logger.debug("section is" + " " + section + " "+ "key is" + " " + key); 38 Config config = new Config(); 39 config.setMultiSection(true); 40 String reValue = ini.get(section, key, String.class); 41 if (reValue == null) { 42 logger.debug("reValue is null"); 43 return -1; 44 } 45 value.set(reValue); 46 return 0; 47 48 } 49 50 @Override 51 public int getkey(String section, String key, CDouble value) { 52 logger.debug("CFileParaManager getKey CDouble start"); 53 String str = Double.valueOf(value.get()).toString(); 54 CString cString = new CString(); 55 cString.set(str); 56 int num = getkey(section, key, cString); 57 if (num != 0) { 58 logger.debug("CFileParaManager getKey CDouble fail"); 59 return -1; 60 } 61 double val = Double.parseDouble(cString.get()); 62 value.set(val); 63 logger.debug("CFileParaManager getKey CDouble end"); 64 return 0; 65 66 } 67 @Override 68 public Map<String, String> getGeys(String section) { 69 logger.debug("CFileParaManager getGeys section start"); 70 if (section == null || section.length() == 0) { 71 logger.debug("CFileParaManager getGeys section is not normal"); 72 } 73 Map<String, String> sections = ini.get(section); 74 logger.debug("CFileParaManager getGeys section end"); 75 return sections; 76 } 77 @Override 78 public int setKey(String section, String key, String value) { 79 logger.debug("CFileParaManager setKey section start"); 80 if (section == null || key == null || value == null) { 81 logger.debug("setction or key or value is not normal"); 82 return -1; 83 } 84 //ini.add(section, key, value); 85 logger.debug("CFileParaManager setKey section end"); 86 //如果section用的话就是修改 没有就是创建 87 ini.put(section, key, value); 88 //2018/10/9 这段代码不加上修改不起任何作用 89 try { 90 ini.store(); 91 } catch (IOException e) { 92 // TODO Auto-generated catch block 93 e.printStackTrace(); 94 } 95 //ini.get(section, key); 96 97 return 0; 98 } 99 100 }
参考的wiki:
https://www.vdisk.cn/ini4j/(ini4j.jar 资源的汇总)
https://blog.csdn.net/wwkms/article/details/48973307(对properties文件的增删改查)
https://blog.csdn.net/xulingqiang/article/details/25059931(对*ini 文件的操作 两个工具方法 一个原始方法)
https://blog.csdn.net/aaronhadoop/article/details/49994413#(*ini文件操作的入门小demo)
https://www.programcreek.com/java-api-examples/index.php?api=org.ini4j.InvalidFileFormatException(没有看懂 但demo确实很多)
以上是关于对*.ini文件的增删改查的主要内容,如果未能解决你的问题,请参考以下文章