Ini配置文件操作接口

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;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值