public class IniUtil {
private File file;
public IniUtil(String filepath) {
file = new File(filepath);
}
/** 配置文件是否有效 **/
public boolean ifValid() {
return file.exists();
}
/** 读INI中指定标记的行数值 **/
public String readLine(String tab) {
if (file == null || !file.exists())
return null;
String str = "";
List<String> list;
try {
list = FileUtils.readLines(file);
for (int i = 0; i < list.size(); i++) {
str = list.get(i).toString().trim();
if (!"".equalsIgnoreCase(str) && !str.startsWith("#")
&& str.contains("=") && !str.startsWith("\n")) {
String array[] = str.split("=");
if (array.length == 2) {
if (array[0].equalsIgnoreCase(tab))
return array[1];
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** 获取INI配置行数 **/
private int getGrpSize() {
List<String> list;
try {
list = FileUtils.readLines(file);
return list.size();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/** 获取Ini文件中键数组 **/
public String[] getIniKeysArray() {
if (file == null || !file.exists())
return null;
int size = getGrpSize();
String[] keys = new String[size];
String str = "";
List<String> list;
try {
list = FileUtils.readLines(file);
for (int i = 0; i < size; i++) {
str = list.get(i).toString().trim();
if (!"".equalsIgnoreCase(str) && !str.startsWith("#")
&& str.contains("=") && !str.startsWith("\n")) {
String array[] = str.split("=");
if (array.length == 2) {
keys[i] = array[0];
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return keys;
}
/** 获取INI键列表 **/
public ArrayList<String> getIniKeyList() {
if (file == null || !file.exists())
return null;
ArrayList<String> keylist = new ArrayList<String>();
String str = "";
List<String> list;
try {
list = FileUtils.readLines(file);
for (int i = 0; i < list.size(); i++) {
str = list.get(i).toString().trim();
if (!"".equalsIgnoreCase(str) && !str.startsWith("#")
&& str.contains("=") && !str.startsWith("\n")) {
String array[] = str.split("=");
if (array.length == 2) {
keylist.add(array[0]);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return keylist;
}
/** 获取INI文件中键值对 **/
public HashMap<String, String> getIniHashMap() {
HashMap<String, String> iniHs = new HashMap<String, String>();
if (file == null || !file.exists())
return null;
int size = getGrpSize();
String str = "";
List<String> list;
try {
list = FileUtils.readLines(file);
for (int i = 0; i < size; i++) {
str = list.get(i).toString().trim();
if (!"".equalsIgnoreCase(str) && !str.startsWith("#")
&& str.contains("=") && !str.startsWith("\n")) {
String array[] = str.split("=");
if (array.length == 2) {
iniHs.put(array[0], array[1]);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return iniHs;
}
}
Ini配置文件操作接口
最新推荐文章于 2019-05-29 11:32:34 发布