java - ini4j读取文件错误

标签 java exception 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;

有什么想法吗?

最佳答案

简单的 Windows .ini 文件

[main]
window-color = 00ffff
splash = true
[others]
version = 0.1
developer = Jim

从.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.");
 }

 }

关于java - ini4j读取文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496233/

相关文章:

java - 由于 keystore 文件导致 ssl 握手异常

android - android中的错误处理。由 : java. lang.IllegalArgumentException 引起的异常:provider=gps

c++ - 为哈希表获取和设置正确重载 [bracket] 运算符

java - 异常处理不起作用

java - 将 UTF8 与 ini4j 一起使用

java - 如何使用ini4j编写ini文件

java - ini4j 存储方法更改注释字符

java - 如何使用 POI 获取 Excel 中列的最大长度

java - Spring 对象映射器 covertValue 方法不转换某些字段

java - JPanel 重绘无法正常工作