ini问题

varick_zhong 2011-06-17 11:01:54
/** */ /**
* 修改ini配置文件中变量的值
* @param file 配置文件的路径
* @param section 要修改的变量所在段名称
* @param variable 要修改的变量名称
* @param value 变量的新值
* @throws IOException 抛出文件操作可能出现的io异常
*/
public static boolean setProfileString(
String file,
String section,
String variable,
String value)
throws IOException {
String fileContent, allLine,strLine, newLine, remarkStr;
String getValue;
System.out.println(file);
BufferedReader bufferedReader = new BufferedReader( new FileReader(file));
boolean isInSection = false ;
fileContent = "" ;
try {

while ((allLine = bufferedReader.readLine()) != null ) {
allLine = allLine.trim();
if (allLine.split( "[;] " ).length > 1 )
remarkStr = ";" + allLine.split( " ; " )[ 1 ];
else
remarkStr = "";
strLine = allLine.split( " ; " )[ 0 ];
Pattern p;
Matcher m;
p = Pattern.compile( " file://[//s*.*//s*//] " );
m = p.matcher((strLine));
if (m.matches()) {
p = 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( "=" );
getValue = strArray[ 0 ].trim();
if (getValue.equalsIgnoreCase(variable)) {
newLine = getValue + "=" + value + " " + remarkStr;
fileContent += newLine + " \r\n " ;
while ((allLine = bufferedReader.readLine()) != null ) {
fileContent += allLine + " \r\n " ;
}
bufferedReader.close();
BufferedWriter bufferedWriter =
new BufferedWriter( new FileWriter(file, false ));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close();

return true ;
}
}
fileContent += allLine + " \r\n " ;
}
} catch (IOException ex) {
throw ex;
} finally {
bufferedReader.close();
}
return false ;
}

/** */ /**
* 程序测试
*/
public static void main(String[] args) {
try {
System.out.println(ConfigurationFile.setProfileString( "d:/1.ini" , "11" , "22" , "111" ));
} catch (IOException e) {
System.out.println(e.toString());
}
}

d:/1.ini为
[11]
22=xx

代码是网上找的,基本上网上都是这个代码,为什么每次返回都是false
不太懂JAVA,求教!
...全文
112 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqfddqr 2011-06-17
  • 打赏
  • 举报
回复
怎么记得有个读ini的类啊 这么费劲
LoongMint 2011-06-17
  • 打赏
  • 举报
回复
解决方案是什么,大家共享下吧!
龙四 2011-06-17
  • 打赏
  • 举报
回复
只有我接分
varick_zhong 2011-06-17
  • 打赏
  • 举报
回复
已解决,散分
varick_zhong 2011-06-17
  • 打赏
  • 举报
回复
自己顶一个

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧