2015-11-03 116 views
1

我正在嘗試使用ini4j。但我無法讀取文件。代碼:ini4j讀取文件錯誤

ini = new Wini(new InputStreamReader(new FileInputStream("./setup.ini"), "UTF-8")); 

但它給我的錯誤:

Unhandled exception type IOException 
Unhandled exception type InvalidFileFormatException 
Unhandled exception type UnsupportedEncodingException 
Unhandled exception type FileNotFoundException 

我已經嘗試過 「C:\ setup.ini的」 和 「的setup.ini」 和「C:/setup.ini 「 我也試過:

ini = new Wini(new InputStreamReader(new FileInputStream(New File("./setup.ini")), "UTF-8")); 

的變量INI正確聲明:

Wini ini; 

任何想法?

回答

0

簡單的Windows .ini文件

從.ini文件讀取

import java.io.File; 
import java.io.IOException; 

import org.ini4j.InvalidFileFormatException; 
import org.ini4j.Wini; 


public class Main { 

public static void main(String args[]){ 
    try{ 
    Wini ini; 
    /* Load the ini file. */ 
    ini = new Wini(new File("config/settings.ini")); 
    /* Extract the window color value.*/ 
    int windowColor = ini.get("main", "window-color", int.class); 
    /* Extract the splash screen status. */ 
    boolean splashScreen = ini.get("main", "splash", boolean.class); 

    /* Show to user the output. */ 
    System.out.println("Your default window color is: " + windowColor); 
    if(splashScreen){ 
    System.out.println("You have your splash screen activated."); 
    }else{ 
    System.out.println("You have your splash disabled."); 
    } 
    } catch (InvalidFileFormatException e) { 
    System.out.println("Invalid file format."); 
    } catch (IOException e) { 
    System.out.println("Problem reading file."); 
    } 
} 

} 

寫入.ini文件

import java.io.File; 
import java.io.IOException; 

import org.ini4j.InvalidFileFormatException; 
import org.ini4j.Wini; 


public class Main { 

public static void main(String args[]){ 
Wini ini; 
try { 
    ini = new Wini(new File("config/settings.ini")); 
    ini.put("main", "window-color", 000000); 
    ini.put("main", "splash", false); 
    ini.store(); 
} catch (InvalidFileFormatException e) { 
    System.out.println("Invalid file format."); 
} catch (IOException e) { 
    System.out.println("Problem reading file."); 
} 

} 
+0

謝謝!缺少的try catch塊是主要問題! –