java操作ini文件

根据网上其他代码进行修改。


读取文件:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class IniReader {

	protected Map<String, Map<String, String>> sections = new LinkedHashMap<String, Map<String, String>>();
	private transient String currentSecion;
	private transient Map<String, String> current;
	
	private String[] notes = new String[]{";", "#", "//"};

	public IniReader(String filename) throws IOException {
		BufferedReader reader = null;
		try {
			File file = new File(filename);
			if(file.exists() && file.isFile()){
				reader = new BufferedReader(new FileReader(file));
				read(reader);
				reader.close();
			}
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}

	protected void read(BufferedReader reader) throws IOException {
		String line;
		while ((line = reader.readLine()) != null) {
			parseLine(line);
		}
	}

	protected void parseLine(String line) {
		line = line.trim();
		if (line.matches("\\[.*\\]")) {
			currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
			current = new LinkedHashMap<String, String>();
			sections.put(currentSecion.trim(), current);
		} else if (line.matches(".*=.*") && !line.startsWith("#")) {
			if (current != null) {
				//去掉注释
				for(String str : notes){
					int num = line.indexOf(str);
					if(num != -1){
						line = line.substring(0, num);
					}
				}
				if(line.length() > 0 && line.indexOf("=") != -1){
					String[] kv = line.split("=", 2);
					current.put(kv[0].trim(), kv[1].trim());
				}
			}
		}
	}

	public String getValue(String section, String name) {
		Map<String, String> sectionmap = sections.get(section);

		if (sectionmap == null) {
			return null;
		}

		String value = sectionmap.get(name);
		return value;
	}
	
	public Set<String> sectionKeys(){
		return sections.keySet();
	}
	
	public Map<String, String> getValues(String section){
		return sections.get(section);
	}

	public boolean containsKey(String section, String key) {
		Map<String, String> m = sections.get(section);
		if(m != null){
			return m.get(key) == null;
		}
		return false;
	}
	
	public Map<String, Map<String, String>> getSections(){
		return sections;
	}

//	public static void main(String[] args) {
//		try {
//			IniReader r = new IniReader("C:/Users/dell/Desktop/aa.ini");
			String val = r.getValue("SET", "DBNAME");
//			Set<String> keys = r.sectionKeys();
//			for(String k : keys){
//				System.out.println("[" + k + "]");
//				Map<String, String> p = r.getValues(k);
//				for(Map.Entry<String, String> entry : p.entrySet()){
//					System.out.println(entry.getKey() + "==" + entry.getValue());
//				}
//			}
//			
//			Map<String, Map<String, String>> sections = r.getSections();
//			new IniWriter("C:/Users/dell/Desktop/bb.ini", sections);
//			
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

}

写文件:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

public class IniWriter {
	
	private StringBuffer sb = new StringBuffer();
	
	public static String LINE_SEPARATOR = System.getProperty("line.separator");

	public IniWriter(String filename, Map<String, Map<String, String>> sections) throws IOException {
		BufferedWriter writer = null;
		try {
			writer = initWriter(filename);
			initIni(sections);
			writer.write(sb.toString());
		} finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	//初始化流
	private BufferedWriter initWriter(String filename) throws IOException{
		File file = new File(filename);
		BufferedWriter writer = null;
		writer = new BufferedWriter(new FileWriter(file, false));
		return writer;
	}
	
	//组装ini文件
	private void initIni(Map<String, Map<String, String>> sections){
		for(Map.Entry<String, Map<String, String>> e : sections.entrySet()){
			String k = e.getKey();
			Map<String, String> values = e.getValue();
			sb.append("[" + k + "]");
			sb.append(LINE_SEPARATOR);
			for(Map.Entry<String, String> ev : values.entrySet()){
				sb.append(ev.getKey() + "=" + ev.getValue());
				sb.append(LINE_SEPARATOR);
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值