javaini,JAVAinit

本文目錄一覽:

編寫一個java程序讀取Windows目錄下的win.ini文件,並輸出內容

try {

File file = new File(“c:/Windows/win.ini”);

FileReader isr = new FileReader(file);

BufferedReader br = new BufferedReader(isr);

String line = null;

while((line = br.readLine()) != null){

System.out.println(line);

}

br.close();

isr.close();

} catch (Exception e) {

e.printStackTrace();

}

java怎麼通過讀取ini文件來確定要輸去文件的路徑

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

public class IniFileReader 

{

    /**

     * 讀取文件制定行

     * @param filePath 文件路徑

     * @param key 文件中配置項的key 如:path = C:/aaa 中的”path = “(注意空格,以具體配置項為準)

     * @return String 制定的配置項的配置數據 如:path = C:/aaa 中的”C:/aaa”

     * @throws Exception

     */

    public static String readerSpcifyLine(String filePath,String key) throws Exception

    {

     StringBuffer result = new StringBuffer();

    

     FileInputStream fis = new FileInputStream(new File(filePath));

     BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

     String line = “”;

     for(line = reader.readLine(); (line != null  !””.equals(line)); line = reader.readLine())

     {

     if(line.startsWith(key))

     {

     result.append(line.substring(line.indexOf(key) + key.length(), line.length()));

     break;

     }

     }

     fis.close();

     reader.close();

     return result.toString();

    }

    

    public static void main(String[] args) throws Exception

    {

     String filePath = “F:/document/path.ini”;

     String key = “path = “;

     String configFilePath = readerSpcifyLine(filePath,key);

     System.out.println(configFilePath);

    }

}

上面這個類應該能幫到你,上面有一個文件讀取方法和一個main測試函數,可以讀出你所說的配置項的信息,方法的說明看代碼注釋。

Java中如何設置讀取ini配置文件?

// 讀取一般的屬性文件

FileInputStream fin=new FileInputStream(“my.ini”); // 打開文件

Properties props=new Properties();                 // 建立屬性類

props.load(fin);                                   // 讀入文件

fin.close();                                       // 關閉文件

java ini文件是什麼文件

準確的說是初始化配置文件,通俗的講就是對你來說沒什麼用處的文件,它裡面會有一些配置信息,比如路徑相關,版本相關等等。

望採納。

java 向ini配置文件中寫入值。

Java讀取和修改ini配置文件,參考如下:

/*

* ConfigurationFile.java

*

*/

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 這是個配置文檔操作類,用來讀取和配置ini配置文檔

* @author 由月

* @version 2004-08-18

* @修改 2008-05-22

*/

public final class ConfigurationFile {

/**

* 從ini配置文檔中讀取變數的值

* @param file 配置文檔的路徑

* @param section 要獲取的變數所在段名稱

* @param variable 要獲取的變數名稱

* @param defaultValue 變數名稱不存在時的默認值

* @return 變數的值

* @throws IOException 拋出文檔操作可能出現的io異常

*/

public static String getProfileString(

String file,

String section,

String variable,

String defaultValue)

throws IOException {

String strLine, value = “”;

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

boolean isInSection = false;

try {

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

strLine = strLine.trim();

//strLine = strLine.split(“[;]”)[0];

Pattern p;

Matcher m;

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

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(“=”);

if (strArray.length == 1) {

value = strArray[0].trim();

if (value.equalsIgnoreCase(variable)) {

value = “”;

return value;

}

} else if (strArray.length == 2) {

value = strArray[0].trim();

if (value.equalsIgnoreCase(variable)) {

value = strArray[1].trim();

return value;

}

} else if (strArray.length 2) {

value = strArray[0].trim();

if (value.equalsIgnoreCase(variable)) {

value = strLine.substring(strLine.indexOf(“=”) + 1).trim();

return value;

}

}

}

}

} finally {

bufferedReader.close();

}

return defaultValue;

}

我導入別人Java項目,ini配置文件里的中文都是 \u開頭的怎麼辦?

這是防止中文亂碼的操作

是正常的,可以直接讀取

如果你想直觀的看

開始運行cmd

native2ascii -reverse

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270714.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-16 13:38
下一篇 2024-12-16 13:38

相關推薦

  • 關於javainit的信息

    本文目錄一覽: 1、java 中init 方法是幹嘛用的 啊 2、init在java中是什麼意思? 3、java中init是什麼意思 java 中init 方法是幹嘛用的 啊 Ap…

    編程 2024-10-04

發表回復

登錄後才能評論