读写ini

/*
* 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();
}

}

这个类是根据网上别人给出的一个类修改而成。原来的类在使用上有一定的局限性,修改以后,增强了其通用性。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值