java读写ini配置文件的示例代码怎么编写

java读写ini配置文件的示例代码怎么编写

本篇文章为大家展示了java读写ini配置文件的示例代码怎么编写,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

下面通过代码先看下java 读写 ini 配置文件,代码如下所示:

java读写ini配置文件的示例代码怎么编写

packageorg.fh.util;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.net.URLDecoder;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/***说明:ini文件读写操作工具类*作者:FHAdmin*from:fhadmin.cn*/publicclassIniFileUtil{/***从ini配置文件中读取变量的值*@paramfile配置文件的路径*@paramsection要获取的变量所在段名称*@paramvariable要获取的变量名称*@paramdefaultValue变量名称不存在时的默认值*@return变量的值*@throwsIOException抛出文件操作可能出现的io异常*/publicstaticStringreadCfgValue(Stringfile,Stringsection,Stringvariable,StringdefaultValue)throwsIOException{StringstrLine,value="";BufferedReaderbufferedReader=newBufferedReader(newFileReader(URLDecoder.decode(file,"UTF-8")));//解决中文路径乱码booleanisInSection=false;try{while((strLine=bufferedReader.readLine())!=null){strLine=strLine.trim();strLine=strLine.split("[;]")[0];Patternp;Matcherm;p=Pattern.compile("\\[\\w+]");//Pattern.compile("file://[//s*.*//s*//]");m=p.matcher((strLine));if(m.matches()){p=Pattern.compile("\\["+section+"\\]");//Pattern.compile("file://[//s*"+section+//"file://s*//]");m=p.matcher(strLine);if(m.matches()){isInSection=true;}else{isInSection=false;}}if(isInSection==true){strLine=strLine.trim();String[]strArray=strLine.split("=");if(strArray.length==1){value=strArray[0].trim();if(value.equalsIgnoreCase(variable)){value="";returnvalue;}}elseif(strArray.length==2){value=strArray[0].trim();if(value.equalsIgnoreCase(variable)){value=strArray[1].trim();returnvalue;}}elseif(strArray.length>2){value=strArray[0].trim();if(value.equalsIgnoreCase(variable)){value=strLine.substring(strLine.indexOf("=")+1).trim();returnvalue;}}}}}finally{bufferedReader.close();}returndefaultValue;}/***修改ini配置文件中变量的值*@paramfile配置文件的路径*@paramsection要修改的变量所在段名称*@paramvariable要修改的变量名称*@paramvalue变量的新值*@throwsIOException抛出文件操作可能出现的io异常*/publicstaticbooleanwriteCfgValue(Stringfile,Stringsection,Stringvariable,Stringvalue)throwsIOException{StringfileContent,allLine,strLine,newLine;StringgetValue=null;BufferedReaderbufferedReader=newBufferedReader(newFileReader(URLDecoder.decode(file,"UTF-8")));//解决中文路径乱码booleanisInSection=false;booleancanAdd=true;fileContent="";try{while((allLine=bufferedReader.readLine())!=null){allLine=allLine.trim();strLine=allLine.split(";")[0];Patternp;Matcherm;p=Pattern.compile("\\[\\w+]");m=p.matcher((strLine));if(m.matches()){p=Pattern.compile("\\["+section+"\\]");m=p.matcher(strLine);if(m.matches()){isInSection=true;}else{isInSection=false;}}if(isInSection==true){strLine=strLine.trim();String[]strArray=strLine.split("=");getValue=strArray[0].trim();if(getValue.equalsIgnoreCase(variable)){newLine=getValue+"="+value;fileContent+=newLine;while((allLine=bufferedReader.readLine())!=null){fileContent+="\r\n"+allLine;}bufferedReader.close();canAdd=false;BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(file,false));bufferedWriter.write(fileContent);bufferedWriter.flush();bufferedWriter.close();returntrue;}}fileContent+=allLine+"\r\n";}if(canAdd){Stringstr=variable+"="+value;fileContent+=str+"\r\n";BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(file,false));bufferedWriter.write(fileContent);bufferedWriter.flush();bufferedWriter.close();}}catch(IOExceptionex){throwex;}finally{bufferedReader.close();}returnfalse;}publicstaticvoidmain(String[]args){try{/*;文件事例[Client];客户端版本号version=0001;设备号devNum=6405*/Stringvalue=IniFileUtil.readCfgValue("L:/a.ini","Client","devNum","1");System.out.println(value);}catch(IOExceptione){e.printStackTrace();}}}

补充:下面看下java 读取ini配置文件

在maven项目中的pom.xml中配置:

<dependency><groupId>org.ini4j</groupId><artifactId>ini4j</artifactId><version>0.5.4</version></dependency>

env.ini文件:

[dev]url="dev-url"user="dev-user"password="dev-password"[testing]url=""user=""password=""

代码:

importorg.ini4j.Ini;importorg.ini4j.Profile;importorg.ini4j.Wini;importjava.io.File;importjava.util.Map;importjava.util.Set;publicclassIniUtils{publicstaticvoidmain(String[]args){try{readIni();}catch(Exceptione){System.out.println(e.getMessage());}}privatestaticvoidreadIni()throwsException{Winiini=newWini(newFile("yourPath/env.ini"));//readIni.Sectionsection=ini.get("dev");Stringurl=section.get("url");Stringuser=section.get("user");Stringpassword=section.get("password");System.out.println(url);System.out.println(user);System.out.println(password);//orjustusejava.util.MapinterfaceMap<String,String>map=ini.get("dev");Stringurl1=map.get("url");Stringuser1=map.get("user");Stringpassword1=map.get("password");System.out.println(url1);System.out.println(user1);System.out.println(password1);//getallsectionnames//Set<String>sectionNames=ini.keySet();//for(StringsectionName:sectionNames){//Profile.Sectionsection1=ini.get(sectionName);//}//writeini.put("sleepy","age",55);ini.put("sleepy","weight",45.6);ini.store();}}

上述内容就是java读写ini配置文件的示例代码怎么编写,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

发布于 2022-01-10 23:48:08
收藏
分享
海报
0 条评论
54
上一篇:实现VMware虚拟机走主机代理上网的步骤是什么 下一篇:java实现五子棋小游戏的代码怎么写
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码