⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inireader.java

📁 一个简易的java画图软件
💻 JAVA
字号:
package skin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;

/**
 * 配置文件读取器
 * 
 * @author Thihy
 * 
 */
public class IniReader {
	protected HashMap<String, String> hashmap = null;
	private String filePath;
	private URL url;
	
	/**
	 * 指定待解析的文件路径
	 * @param filePath
	 */
	public IniReader(String filePath) {
		this.filePath = filePath;
		url = getClass().getResource(filePath);
	}
	/**
	 * 解析指定文件,放到映射表中
	 * @throws IOException 
	 */
	public void parser() throws IOException {
		InputStreamReader isr = new InputStreamReader(url.openStream());
		BufferedReader reader = new BufferedReader(isr);
		hashmap = new HashMap<String, String>();
		String line;
		String currentSection = "";
		while ((line = reader.readLine()) != null) {
			line = line.trim();
			if (line.startsWith(";") || line.equals(""))
				continue;// 是注释或为空
			if (line.matches("\\[.*\\]")) {// 是分项名
				currentSection = line.replaceFirst("\\[(.*)\\]", "$1");
				continue;
			}
			if (line.matches(".*=.*")) { // 是属性项
				int i = line.indexOf('=');
				String name = line.substring(0, i);
				String value = line.substring(i + 1);
				name = name.trim();
				value = value.trim();
				// hashmap中的key为分项名+点号+属性名
				hashmap.put(currentSection + "." + name, value);
			}
		}

		reader.close();
		// is.close();
	}
	/**
	 * 获取解析后的映射表
	 * @return 映射表
	 * @throws IOException
	 */
	public HashMap<String, String> getMap() throws IOException {
		if (hashmap == null)
			parser();
		return hashmap;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -