第二篇: Ini 文件解析器
来源:互联网 发布:mac设置输入法切换 编辑:程序博客网 时间:2024/05/16 14:55
解析Windows的Ini文件。
- /*******************************************************************************
- * QIniFile.java
- *
- * Created: 2008-5-4 16:14:18 by @author GerryHua
- *
- * History:
- *
- *******************************************************************************/
- package jiamu.base.other;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.Vector;
- import jiamu.base.exceptions.QException;
- import jiamu.base.functions.StringFunctions;
- import jiamu.base.functions.SystemFunctions;
- /**
- * A class to handle the INI file.
- */
- public class QIniFile {
- /**
- * A class to implement the section in INI file.
- */
- private class INISection {
- /**
- * The name of the section.
- */
- private String name = "";
- /**
- * The value collection of the section.
- */
- private Vector<INIValue> values = null;
- /**
- * Instantiate a new INI section.
- *
- * @param name
- * the name of the section.
- */
- public INISection(String name) {
- this.setName(name);
- this.values = new Vector<INIValue>();
- }
- /**
- * Add the value. Check the value first. If already exists, change the
- * old value to the new value.
- *
- * @param iniValue
- * the INIValue object.
- *
- * @return the added INI value.
- */
- public INIValue addValue(INIValue iniValue) {
- int pos = this.getValuePosition(iniValue);
- if (pos == -1) {
- this.values.add(iniValue);
- return iniValue;
- } else {
- this.values.get(pos).setValue(iniValue.getValue());
- return this.values.get(pos);
- }
- }
- /**
- * Add the INIValue with a line value.
- *
- * @param lineValue
- * the line value.
- */
- public void addValue(String lineValue) {
- this.values.add(new INIValue(lineValue));
- }
- /**
- * Get the name of the section.
- *
- * @return the name of the section.
- */
- public String getName() {
- return this.name;
- }
- /**
- * Get the value of special key.
- *
- * @param key
- * the key.
- *
- * @return the value.
- */
- public String getValue(String key) {
- for (int i = 0; i < this.values.size(); i++) {
- if (key.compareToIgnoreCase(this.values.get(i).getName()) == 0) {
- return this.values.get(i).getValue();
- }
- }
- return null;
- }
- /**
- * Get the position of the INIValue. Returns -1 if not found.
- *
- * @param iniValue
- * the INIValue object.
- *
- * @return the position in the value collection of the section. Returns
- * -1 if not found.
- */
- private int getValuePosition(INIValue iniValue) {
- int pos = -1;
- for (int i = 0; i < this.values.size(); i++) {
- if (iniValue.getName().compareToIgnoreCase(this.values.get(i).getName()) == 0) {
- pos = i;
- break;
- }
- }
- return pos;
- }
- /**
- * Sets the name of the section.
- *
- * @param name
- * the new name.
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * Get the string of the object like
- * <ul>
- * <li>[Section]</li>
- * <li>key1 = value1</li>
- * <li>key2 = value2</li>
- * </ul>
- *
- * @return the string of the object .
- */
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("[" + this.name + "]");
- builder.append(SystemFunctions.getLineFeed());
- for (int i = 0; i < this.values.size(); i++) {
- builder.append(this.values.get(i).toString());
- builder.append(SystemFunctions.getLineFeed());
- }
- builder.append(SystemFunctions.getLineFeed());
- return builder.toString();
- }
- }
- /**
- * A class to implement the value in INI file.
- */
- private class INIValue {
- /**
- * The name of the INIValue.
- */
- private String name = "";
- /**
- * The value of the INIValue.
- */
- private String value = "";
- /**
- * Instantiate a new INI value.
- *
- * @param lineValue
- * the line value.
- */
- public INIValue(String lineValue) {
- int pos = lineValue.indexOf('=');
- this.name = lineValue.substring(0, pos).trim();
- this.value = lineValue.substring(pos + 1).trim();
- }
- /**
- * Get the name or the key of the INIValue.
- *
- * @return the name or the key.
- */
- public String getName() {
- return this.name;
- }
- /**
- * Get the value of the INIValue.
- *
- * @return the value.
- */
- public String getValue() {
- return this.value;
- }
- /**
- * Set the name of the INIValue.
- *
- * @param name
- * the new name.
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * Set the value of the INIValue.
- *
- * @param value
- * the new value.
- */
- public void setValue(String value) {
- this.value = value;
- }
- /**
- * Get the string of the object like key = value
- *
- * @return the string.
- */
- @Override
- public String toString() {
- return this.name + " = " + this.value;
- }
- }
- /**
- * Get the value of the key in the section in the INI file.
- *
- * @param section
- * the section value.
- * @param key
- * the key value.
- * @param defaultValue
- * the default value.
- * @param fileName
- * the INI file name.
- *
- * @return the value.
- * @throws QException
- * FileNotFoundException or IOException.
- */
- public static String getValue(String section, String key, String defaultValue, String fileName) throws QException {
- QIniFile iniFile = new QIniFile(fileName);
- iniFile.load();
- String value = iniFile.getValue(section, key);
- if (value == null) {
- return defaultValue;
- } else {
- return value;
- }
- }
- /**
- * Sets the value of the key in the section in the INI file.
- *
- * @param section
- * the section value.
- * @param key
- * the key value.
- * @param value
- * the value to be set.
- * @param fileName
- * the ini file name.
- * @throws QException
- * FileNotFoundException or IOException.
- */
- public static void setValue(String section, String key, String value, String fileName) throws QException {
- QIniFile iniFile = new QIniFile(fileName);
- iniFile.load();
- iniFile.setValue(section, key, value);
- iniFile.save();
- }
- /**
- * The file name of the INI file.
- */
- private String fileName = null;
- /**
- * The section collection in the INI file.
- */
- private Vector<INISection> sections = null;
- /**
- * Instantiate a new INI file.
- *
- * @param fileName
- * the file Name.
- *
- * @throws QException
- * FileNotFoundException or IOException.
- */
- public QIniFile(String fileName) throws QException {
- this.fileName = fileName;
- this.sections = new Vector<INISection>();
- this.load();
- }
- /**
- * Add the section. Check if the section exists. If not, add the new
- * section.
- *
- * @param section
- * the section.
- *
- * @return the added INI section.
- */
- private INISection addSection(INISection section) {
- int pos = this.getSectionPosition(section);
- if (pos == -1) {
- this.sections.add(section);
- return section;
- } else {
- return this.sections.get(pos);
- }
- }
- /**
- * Create the file if the file doesn't exist.
- *
- * @throws QException
- * the base exception.
- */
- private void createFile() throws QException {
- File file = new File(this.fileName);
- if (file.exists()) {
- return;
- }
- try {
- file.createNewFile();
- } catch (IOException ex) {
- throw new QException(ex, "QIniFile", "createFile", "");
- }
- }
- /**
- * Get the section position. If not found, return -1.
- *
- * @param section
- * the section.
- *
- * @return the position of the section. If not found, return -1.
- */
- private int getSectionPosition(INISection section) {
- int pos = -1;
- for (int i = 0; i < this.sections.size(); i++) {
- if (section.getName().compareToIgnoreCase(this.sections.get(i).getName()) == 0) {
- pos = i;
- break;
- }
- }
- return pos;
- }
- /**
- * Get the string value of the key in the section.
- *
- * @param sectionValue
- * the section value.
- * @param keyValue
- * the key value.
- * @param defaultValue
- * the default value.
- * @param writeIfNotExists
- * indicates whether to write the value if not found.
- *
- * @return the string value.
- *
- * @throws QException
- * the base exception.
- */
- public String getString(String sectionValue, String keyValue, String defaultValue, boolean writeIfNotExists) throws QException {
- String value = this.getValue(sectionValue, keyValue);
- if (value == null) {
- value = defaultValue;
- if (writeIfNotExists) {
- this.setString(sectionValue, keyValue, value);
- }
- }
- return value;
- }
- /**
- * Get the value of the key in the section.
- *
- * @param section
- * the section value.
- * @param key
- * the key value.
- *
- * @return the value.
- */
- public String getValue(String section, String key) {
- for (int i = 0; i < this.sections.size(); i++) {
- if (section.compareToIgnoreCase(this.sections.get(i).getName()) == 0) {
- return this.sections.get(i).getValue(key);
- }
- }
- return null;
- }
- /**
- * Load the INI file and create the structure.
- *
- * @return true, if successful.
- *
- * @throws QException
- * FileNotFoundException or IOException.
- */
- public boolean load() throws QException {
- try {
- this.createFile();
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(this.fileName), "UTF-8"));
- String lineValue = in.readLine();
- INISection currentSection = null;
- while (lineValue != null) {
- lineValue = lineValue.trim();
- if (!StringFunctions.isEmpty(lineValue)) {
- if (lineValue.startsWith("[")) {
- currentSection = new INISection(lineValue.substring(1, lineValue.length() - 1));
- this.addSection(currentSection);
- } else {
- if (currentSection != null) {
- currentSection.addValue(lineValue);
- }
- }
- }
- lineValue = in.readLine();
- }
- in.close();
- } catch (FileNotFoundException ex) {
- throw new QException(ex, "QIniFile", "load", "");
- } catch (IOException ex) {
- throw new QException(ex, "QIniFile", "load", "");
- }
- return true;
- }
- /**
- * Save the file with the current structure.
- *
- * @return true, if successful.
- *
- * @throws QException
- * the IOException.
- */
- public boolean save() throws QException {
- try {
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.fileName), "UTF-8"));
- out.append(this.toString());
- out.close();
- } catch (UnsupportedEncodingException ex) {
- throw new QException(ex, "QIniFile", "save", "");
- } catch (FileNotFoundException ex) {
- throw new QException(ex, "QIniFile", "save", "");
- } catch (IOException ex) {
- throw new QException(ex, "QIniFile", "save", "");
- }
- return true;
- }
- /**
- * Set the string value of the key in the section.
- *
- * @param sectionValue
- * the section value.
- * @param keyValue
- * the key value.
- * @param setValue
- * the value to be set.
- *
- * @throws QException
- * the base exception.
- */
- public void setString(String sectionValue, String keyValue, String setValue) throws QException {
- this.setValue(sectionValue, keyValue, setValue);
- this.save();
- }
- /**
- * Set the value of the key in the section.
- *
- * @param section
- * the section value.
- * @param key
- * the key value.
- * @param value
- * the value to be set.
- */
- public void setValue(String section, String key, String value) {
- INISection iniSection = new INISection(section);
- iniSection = this.addSection(iniSection);
- iniSection.addValue(new INIValue(key + "=" + value));
- }
- /**
- * Get the string of the object like the INI file content.
- *
- * @return the string value.
- */
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < this.sections.size(); i++) {
- builder.append(this.sections.get(i).toString());
- }
- return builder.toString();
- }
- }
完。
PS:这次使用Opera,非常顺手。
- 第二篇: Ini 文件解析器
- ini文件解析器
- ini文件解析器
- python 解析ini 文件
- php解析.ini文件
- INI文件解析类
- shell解析ini文件
- python解析ini文件
- shell解析ini文件
- Java解析INI文件
- node解析ini文件
- Go-ini文件解析
- Python解析INI文件
- INI文件解析、遍历
- ini文件解析中的STL
- C# ini 文件解析库
- 状态机学习 解析INI文件
- ini文件解析器之纯C语言实现
- ubuntu8.04 清理无用文件 释放硬盘空间
- Mina研究笔记
- 数组(待续)
- 菜鸟初学技术篇(之一)
- [转贴]窗口应用技巧(1)
- 第二篇: Ini 文件解析器
- 口琴 语录 . 经典
- [转贴]谈谈父窗口和所有者窗口
- 时间的魔力
- 强制解决jsp输出页面的乱码
- 关于微软最近黑屏盗版操作系统的一点思考
- 好久没写了,沉浸在Flex的艺术当中
- 同事的总结:垃圾回收不回收
- 智能Oracle11g帮助成长型企业展翅高飞