yonge 4 vuotta sitten
vanhempi
commit
382b3aa039

+ 7 - 1
edu-util/pom.xml

@@ -94,11 +94,17 @@
 			<groupId>javax.servlet</groupId>
 			<artifactId>javax.servlet-api</artifactId>
 		</dependency>
-		
+
 		<dependency>
 			<groupId>net.coobird</groupId>
 			<artifactId>thumbnailator</artifactId>
 			<version>0.4.11</version>
 		</dependency>
+
+		<dependency>
+			<groupId>org.ini4j</groupId>
+			<artifactId>ini4j</artifactId>
+			<version>0.5.4</version>
+		</dependency>
 	</dependencies>
 </project>

+ 41 - 0
edu-util/src/main/java/com/keao/edu/util/iniFile/IniFileEntity.java

@@ -0,0 +1,41 @@
+package com.keao.edu.util.iniFile;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+
+public class IniFileEntity {
+
+	private String section;
+
+	private String key;
+
+	private String value;
+
+	public String getSection() {
+		return section;
+	}
+
+	public void setSection(String section) {
+		this.section = section;
+	}
+
+	public String getKey() {
+		return key;
+	}
+
+	public void setKey(String key) {
+		this.key = key;
+	}
+
+	public String getValue() {
+		return value;
+	}
+
+	public void setValue(String value) {
+		this.value = value;
+	}
+	
+	@Override
+	public String toString() {
+		return ToStringBuilder.reflectionToString(this);
+	}
+}

+ 61 - 0
edu-util/src/main/java/com/keao/edu/util/iniFile/IniFileUtil.java

@@ -0,0 +1,61 @@
+package com.keao.edu.util.iniFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.ini4j.Ini;
+import org.ini4j.InvalidFileFormatException;
+import org.ini4j.Profile.Section;
+
+import com.keao.edu.util.exception.UtilException;
+
+public class IniFileUtil {
+
+	/**
+	 * 读取Ini文件
+	 * @param iniFile 文件绝对路径(文件编码须UTF-8)
+	 * @return 返回所有section以及下面的key/value
+	 * @throws InvalidFileFormatException
+	 * @throws IOException
+	 */
+	public static Map<String, List<IniFileEntity>> readIniFile(File iniFile) throws InvalidFileFormatException, IOException {
+		if (!iniFile.exists()) {
+			throw new UtilException("文件[" + iniFile.getAbsolutePath() + "]不存在");
+		}
+		Ini ini = new Ini();
+		ini.load(iniFile);
+
+		Map<String, List<IniFileEntity>> result = new HashMap<String, List<IniFileEntity>>();
+		IniFileEntity entity = null;
+
+		Collection<Section> sections = ini.values();
+		for (Section section : sections) {
+			List<IniFileEntity> entryList = result.get(section.getName());
+			if (entryList == null) {
+				entryList = new ArrayList<IniFileEntity>();
+			}
+			for (Entry<String, String> entry : section.entrySet()) {
+				entity = new IniFileEntity();
+				entity.setSection(section.getName());
+				entity.setKey(entry.getKey());
+				entity.setValue(entry.getValue());
+
+				entryList.add(entity);
+			}
+			result.put(section.getName(), entryList);
+		}
+
+		return result;
+	}
+	
+	public static void main(String[] args) throws InvalidFileFormatException, IOException {
+		File file = new File("e:/test.ini");
+		System.out.println(IniFileUtil.readIniFile(file));
+	}
+}