日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

java 修改ini文件_Java读取和修改ini配置文件

發(fā)布時(shí)間:2025/3/12 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 修改ini文件_Java读取和修改ini配置文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

/**

* 修改ini配置文檔中變量的值

* @param file 配置文檔的路徑

* @param section 要修改的變量所在段名稱

* @param variable 要修改的變量名稱

* @param value 變量的新值

* @throws IOException 拋出文檔操作可能出現(xiàn)的io異常

*/

public static boolean setProfileString(

String file,

String section,

String variable,

String value)

throws IOException {

String fileContent, allLine,strLine, newLine, remarkStr;

String getValue;

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

boolean isInSection = false;

fileContent = "";

try {

while ((allLine = bufferedReader.readLine()) != null) {

allLine = allLine.trim();

System.out.println("allLine == "+allLine);

strLine = allLine;

Pattern p;

Matcher m;

p = Pattern.compile("\\["+section+"\\]");

m = p.matcher((strLine));

//System.out.println("+++++++ ");

if (m.matches()) {

System.out.println("+++++++ ");

p = Pattern.compile("\\["+section+"\\]");

m = p.matcher(strLine);

if (m.matches()) {

System.out.println("true ");

isInSection = true;

} else {

isInSection = false;

System.out.println("+++++++ ");

}

}

if (isInSection == true) {

strLine = strLine.trim();

String[] strArray = strLine.split("=");

getValue = strArray[0].trim();

if (getValue.equalsIgnoreCase(variable)) {

// newLine = getValue + " = " + value + " " + remarkStr;

newLine = getValue + " = " + value + " ";

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) {

//String value = Config.getProfileString("sysconfig.ini", "Option", "OracleDB", "default");

//System.out.println(value);

try {

System.out.println("值已經(jīng)改變!... "+ConfigurationFile.setProfileString("E:/web5/h2_test/gamewww.ini", "TestSect1", "10012", "111111"));

System.out.println("值讀取成功!... "+ConfigurationFile.getProfileString("E:/web5/h2_test/gamewww.ini", "TestSect1", "10012", ""));

} catch (IOException e) {

System.out.println("錯(cuò)誤 ......"+e.toString());

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java 修改ini文件_Java读取和修改ini配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。