eclipse Android 读取INI文件
时间: 2023-07-18 14:07:10 浏览: 56
Android 平台上没有 natively 支持读取 INI 文件的 API。你需要自己实现读取 INI 文件的功能。
以下是一个简单的读取 INI 文件的示例代码:
```java
public class IniReader {
private HashMap<String, HashMap<String, String>> iniMap;
public IniReader(String filePath) {
iniMap = new HashMap<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filePath));
String line;
String section = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
section = line.substring(1, line.length() - 1);
} else if (section != null && line.matches(".*=.*")) {
String[] keyValue = line.split("=", 2);
if (!iniMap.containsKey(section)) {
iniMap.put(section, new HashMap<String, String>());
}
iniMap.get(section).put(keyValue[0].trim(), keyValue[1].trim());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String getValue(String section, String key) {
if (iniMap.containsKey(section)) {
return iniMap.get(section).get(key);
}
return null;
}
}
```
使用示例:
```java
IniReader iniReader = new IniReader("/sdcard/myfile.ini");
String value = iniReader.getValue("Section1", "Key1");
```
请注意,这是一个简单的实现。如果你需要处理复杂的 INI 文件,你需要做更多的工作去解析它们。