读写ini
来源:互联网 发布:wifi监控摄像头软件 编辑:程序博客网 时间:2024/04/29 12:35
/** File: operate the INI file.* * Notice: One variable only can read one file, if you want to read more files,* please use more variable to read them.* If the variable has read one file, then it can't read another file.*/package ... ...import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Properties;import android.util.Log;public class INIReader {protected HashMap sections = new LinkedHashMap();private String currentSecion;private Properties current;private String fileName = null;public INIReader() {}/** Description: read the data from file.* Parameter: the file name.* Return: the read line number. return zero if file not found or empty. */public int read(String argFileName) { int read_line = 0; if (fileName != null) { return read_line; } fileName = argFileName; try { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(argFileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { read_line++; parseLine(line); } reader.close(); } catch (Exception e) { e.printStackTrace(); Log.e("EncryptBox", "INIReader: read error."); } return read_line;}/** Description: parse one line which read from file.* Parameter: the line.* Return: NONE. */protected void parseLine(String line) { line = line.trim(); if (line.matches("\\[.*\\]")) { currentSecion = line.replaceFirst("\\[(.*)\\]", "$1"); current = new Properties(); } else if (line.matches(".*=.*")) { int i = line.indexOf('='); String name = line.substring(0, i); String value = line.substring(i + 1); current.setProperty(name.trim(), value.trim()); sections.put(currentSecion, current); }}/** Description: get value by section and key.* Parameter: section and key.* Return: the value. return null if not found.*/public String getValue(String section, String key) { if (fileName == null) { return null; } Properties p = (Properties) sections.get(section); if (p == null) { return null; } String value = p.getProperty(key); //Log.d("EncryptBox", "get value="+value); return value;}/** Description: update value.* Parameter: section, key and value.* flagAdd: true: add it if the data not exist; * false: return false if the data not exist.* Return: true: change success, * false: change fail.* Notice: we don't write data into file in this function.*/public boolean changeProperty(String sectionName,String key,String value) { return changeProperty(sectionName, key, value, true);}public boolean changeProperty(String sectionName,String key,String value,boolean flagAdd) { if (key.length() == 0){ return false; } sectionName = sectionName.trim(); Properties p = (Properties)sections.get(sectionName); if (null == p) { if (flagAdd == true) { // not find the section, we should create it. current = new Properties(); current.setProperty(key.trim(), value.trim()); sections.put(sectionName, current); return true; } else { Log.e("EncryptBox", "INIReader: not find section."); return false; } //throw new IllegalArgumentException("sectionName参数出错"); } p.setProperty(key, value); return true;}/** Description: write the data to file.* Parameter: NONE.* Return: true: success.*/public boolean write() { return write(fileName);}public boolean write(String argFileName) { if (argFileName == null) { return false; } PrintWriter writer = null; try { File f = new File(argFileName); if (!f.exists()) { // create file. f.createNewFile(); } writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f))); StringBuilder builder = new StringBuilder(); Iterator<String> iterator = sections.keySet().iterator(); while (iterator.hasNext()) { String sectionName = iterator.next(); Properties p = (Properties)sections.get(sectionName); builder.append("\n").append("\n").append("[").append(sectionName).append("]"); builder.append(getPropertiesString(p)); } writer.write(builder.toString().trim()); writer.flush(); writer.close(); return true; } catch (Exception e) { e.printStackTrace(); Log.e("EncryptBox", "INIReader: write error(" + argFileName + ")."); } if (writer != null) { writer.close(); } return false;}/** Description: get the string of Properties.* Parameter: Properties.* Return: string.*/private String getPropertiesString(Properties p) { StringBuilder builder = new StringBuilder(); Iterator iterator = p.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next().toString(); builder.append("\n").append(key).append("=").append(p.get(key)); } return builder.toString();}}
这个类是根据网上别人给出的一个类修改而成。原来的类在使用上有一定的局限性,修改以后,增强了其通用性。
- 读写INI
- 读写Ini
- 读写ini
- 读写INI
- 读写ini
- 读写ini
- 读写ini
- ini读写
- 读写INI
- C#读写INI文件
- INI的读写
- INI文件的读写
- C#读写INI文件
- C#读写INI文件
- C#读写INI文件
- 读写INI文件
- 如何读写INI 文件
- 读写INI文件
- javascript面向对象!~
- 输入整数的字符串,输出相应的整数
- java frame
- 在一个事务中,hibernate的查询数据,不要在set值,比如:密码解密,否则会重新更新到数据库中
- Actor模型
- 读写ini
- Js 实现打印并去除页眉页脚
- php获取字符串的编码格式的方法(函数)
- JSP 9 大内置对象介绍
- strut2 Result
- 四、数码管使用
- android学习笔记31--------------短信会话列表信息的获取
- JAVA读取ini文件(很简单不带section)
- MCISendString详细参数