Android下对.ini文件的解析

.ini文件时一堆键值对的文件

没有section的解析代码

package com.cj.td.common.util.ini;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import android.content.Context;

public class IniReaderNoSection {
	public Properties properties = null;
	/**
	 * 读取asset文件
	 * @param context
	 * @param iniPath
	 */
	public IniReaderNoSection(Context context,String iniPath){
		try{
			InputStream inputStream = context.getResources().getAssets().open(iniPath);
			properties = new Properties();
			properties.load(inputStream);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	/***
	 * 读取raw文件下的ini文件
	 * @param context
	 * @param resourceId
	 */
	public IniReaderNoSection(Context context, int resourceId) {
		InputStream inputStream = context.getResources().openRawResource(
				resourceId);
		try {
			properties = new Properties();
			properties.load(inputStream);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 读取sd卡文件
	 * @param filename
	 */
	public IniReaderNoSection(String filename) {
		File file = new File(filename);
		try {
			properties = new Properties();
			properties.load(new FileInputStream(file));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 获取某个key的值
	 * @param key
	 * @return
	 */
	public String getIniKey(String key){
		if(!properties.contains(key)){
			return null;
		}
		return String.valueOf(properties.get(key));
	}
}

有section的文件的解析

package com.cj.td.common.util.ini;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.loon.framework.android.game.utils.StringUtils;

import android.content.Context;

public class IniReaderHasSection {
	private Map<String,Properties> sections;
	
	private String section;
	
	private Properties properties;
	
	public IniReaderHasSection(String fileName) throws IOException{
		sections = new HashMap<String, Properties>();
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		read(reader);
		reader.close();
	}
	
	/**
	 * 读取asset文件
	 * @param context
	 * @param iniPath
	 */
	public IniReaderHasSection(Context context,String iniPath){
		try{
			sections = new HashMap<String, Properties>();
			InputStream inputStream = context.getResources().getAssets().open(iniPath);
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			read(reader);
			reader.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	private void read(BufferedReader reader)  throws IOException{
		String line;
		while((line = reader.readLine())!=null){
			parseLine(line);
		}
	}

	private void parseLine(String line) {
		line = line.trim();
		if(line.matches("\\[.*\\]")==true){
			section = line.replaceFirst("\\[(.*)\\]", "$1");
			properties = new Properties();
			sections.put(section, properties);
		}else{
			if(properties!=null
					&&!line.startsWith(";")
					&&!StringUtils.isEmpty(line)){
				int i = line.indexOf('=');
				String name = line.substring(0,i-1).trim();
				String value = line.substring(i+1).trim();
				properties.setProperty(name, value);
			}
		}
	}
	
	public String getValue(String section,String name){
		Properties p = sections.get(section);
		
		if(p == null)return null;
		
		return p.getProperty(name);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值