Java解析INI文件

孙永思
2023-12-01

可以解析带sections的INI文件, 如果不带section,则 section 默认为 default

一个节可以扩展或者通过在节的名称之后带一个冒号(:)和被继承的配置数据的节的名称来从另一个节继承

package com.ini;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class ParseIni {

	protected HashMap<string properties=""> sections = new HashMap<string properties="">();
	private transient String defaultName = "default";
	private transient String sectionName;
	private transient Properties property;
	private Properties parentObj;

	/**
	 * 构造函数
	 * 
	 * @param filename
	 *            文件路径
	 * @throws IOException
	 */
	public ParseIni(String filename) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		read(reader);
		reader.close();
	}

	/**
	 * 文件读取
	 * 
	 * @param reader
	 * @throws IOException
	 */
	protected void read(BufferedReader reader) throws IOException {
		String line;
		sectionName = this.defaultName;
		property = new Properties();
		sections.put(sectionName, property);

		while ((line = reader.readLine()) != null) {
			parseLine(line);
		}
	}

	/**
	 * 解析每行数据
	 * 
	 * @param line
	 */
	protected void parseLine(String line) {
		line = line.trim();
		if (line.indexOf('#') == 0 || line.indexOf(';') == 0) {
			return;
		}

		if (line.matches("\\[.*\\]")) {
			sectionName = line.replaceFirst("\\[(.*)\\]", "$1").trim();
			property = new Properties();
			if (sectionName.matches(".*:.*")) {
				int pos = sectionName.indexOf(':');
				String child = sectionName.substring(0, pos);
				String parent = sectionName.substring(pos + 1);

				parentObj = this.getSection(parent);
				if (parentObj != null) {
					property = (Properties) parentObj.clone();
					sections.put(child, property);
				}
			} else {
				sections.put(sectionName, property);
			}
		} else if (line.matches(".*=.*")) {
			int i = line.indexOf('=');
			String name = line.substring(0, i).trim();
			String value = line.substring(i + 1).trim();

			if (value.indexOf('"') == 0 || value.indexOf('\'') == 0) {
				// 去掉前面符号 " 或 '
				value = value.substring(1, value.length());
				// 去掉后面 " 或 '
				int len = value.length();
				if (value.indexOf('"') == len - 1 || value.indexOf('\'') == len - 1) {
					value = value.substring(0, len - 1);
				}
			}

			property.setProperty(name, value);
		}
	}

	/**
	 * 根据节 和 key 获取值
	 * 
	 * @param section
	 * @param key
	 * @return String
	 */
	public String get(String section, String key) {
		if (section.equals(null) || section == "")
			section = this.defaultName;

		Properties property = (Properties) sections.get(section);
		if (property == null) {
			return null;
		}

		String value = property.getProperty(key);
		if (value == null)
			return null;

		return value;
	}

	/**
	 * 获取节下所有key
	 * 
	 * @param section
	 * @return Properties
	 */
	public Properties getSection(String section) {
		if (section.equals(null) || section == "")
			section = this.defaultName;

		Properties property = (Properties) sections.get(section);
		if (property == null) {
			sections.put(section, property);
		}

		return property;
	}

	/**
	 * 增加节点 及 值
	 * 
	 * @param section
	 * @param property
	 */
	public void set(String section, String key, String value) {
		if (property == null)
			property = new Properties();

		if (section.equals(null) || section == "")
			section = this.defaultName;

		if (key.equals(null) || key == "") {
			System.out.println("key is null");
			return;
		}

		sections.put(section, property);
		property.setProperty(key, value);
	}

	/**
	 * 增加节点
	 * 
	 * @param section
	 */
	public void setSection(String section) {
		sections.put(section, property);
	}
}

</string></string>


文件:app.ini

[app]
#生产环境
env = prod

;应用的位置
app_dir = "/data/app"
;配置文件的位置
config = "/etc/app/config"

# 数据库
database = "app"
# 数据库用户名
user = "root"
# 数据库密码
password = "123456"

# sever1继承app配置
[server1:app]
database = "server1"
user = "server1"
password = "s1passwd"

# sever1继承app配置
[server2:app]
database = "server1"
user = "server1"
password = "s1passwd"
;开发环境
env = dev


调用代码:

String fileName = "app.ini";
ParseIni config = new ParseIni(fileName);
String app = config.get("app", "env");
System.out.println("app.env = " + app);
		
String s1 = config.get("server1", "env");
System.out.println("server1.env = " + s1);
		
String s2 = config.get("server2", "database");
System.out.println("server2.database = " + s2);
		
config.setSection("server3");
Properties node1 = config.getSection("server3");
		
node1.setProperty("env", "local");
System.out.println("server3.env = " + config.get("server3", "env"));

输出内容:

app.env = prod
server1.env = local
server2.database = server1
server3.env = local

这里要感谢:(yuyue618) 
http://blog.csdn.net/yuyue618/article/details/4139086

 类似资料: