一个java操作ini格式文件的工具类,支持增删查改,不过由于项目所需,只做了针对单行的操作。写得不好求指教。

长孙沈义
2023-12-01
package com.other;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Properties;

import com.TouchLife.touchlife.Manager.Global;

/**
 * INI文件操作类。增删查改
 * @author ckw
 * 2014年6月26日
 */
public class ReadIni {
	private static HashMap<String, Properties> iniInfo = new HashMap<String, Properties>();
	private static String currentSecion;
	private static Properties properties;
	public static final String fileUrl = Global.RootPath+"BaseConfig.ini";	 //配置文件的路径
	public ReadIni(){

		try {
			File file = new File(fileUrl);
			if (file.exists()) {
				BufferedReader reader = new BufferedReader(new FileReader(fileUrl));
				read(reader);
			} else {
				file.getParentFile().mkdirs();	// getParentFile()返回File类型的父路径,mkdirs()创建所有的路径					
				file.createNewFile(); // 创建了文件	
		
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

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

	/**
	 * 读取每一行
	 * @param line
	 */
	private void parseLine(String line) {
		line = line.trim();
		if (line.matches("\\[.*\\]") == true) {
			currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
			properties = new Properties();
			iniInfo.put(currentSecion, properties);
		} else if (line.matches(".*=.*") == true) {
			if (properties != null) {
				int i = line.indexOf('=');
				String key = line.substring(0, i);
				String value = line.substring(i + 1);
				properties.setProperty(key, value);
				
			}
		}
	}

	/**
	 * 根据节、键名获取值
	 * @param section	节名
	 * @param key		键名
	 * @return
	 */
	public String getValue(String section, String key) {
		Properties p = (Properties) iniInfo.get(section);
		if (p == null) {
			return null;
		}
		String value = p.getProperty(key);
		return value;
	}
	
	
	/**
	* 新增一行。新增之前判断是否存在,如果存在就修改值。若不存在则新增。
	* 
	* @param section
	*            要修改的变量所在段名称
	* @param variable
	*            要修改的变量名称
	* @param value
	*            变量的新值
	*/
	public  void setValue(String section,String variable, String value){
		if(getValue(section, variable) == null){
			try {
				addValue(section, variable, value);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(getValue(section,variable).equals("")){
			try {
				updateValue(section, variable, value);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			System.out.println("已存在");
			return;
		}
	}

	public static boolean addValue(String section,String variable, String value) throws IOException {
		String fileContent, line, newLine;
		BufferedReader bufferedReader = new BufferedReader(new FileReader(fileUrl));
		fileContent = "";
	
		try {
			while ((line = bufferedReader.readLine()) != null) {
				line = line.trim();
				if (line.matches("\\[.*\\]") == true) {
					if(line.replaceFirst("\\[(.*)\\]", "$1").equals(section)){
						newLine = line+"\r\n"+variable + "=" + value;
						fileContent += newLine +"\r\n";
					
						while ((line = bufferedReader.readLine()) != null) {
							
							fileContent += line + "\r\n";
						}
						//定义编码
			            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(fileUrl,false), "UTF-8");		   
						BufferedWriter bufferedWriter = new BufferedWriter(write);
						bufferedWriter.write(fileContent);
						bufferedWriter.flush();
						bufferedWriter.close();
						System.out.println("插入成功");
						return true;
						//continue;
					}else{
						fileContent += line + "\r\n";
						continue;
					}	
				
				}
				fileContent += line + "\r\n";
				
			
			}
		} catch (IOException ex) {
			throw ex;
		} finally {
			bufferedReader.close();
		}
		return false;
	}
	
	/**
	* 修改ini配置文件中变量的值
	* 
	* @param section
	*            要修改的变量所在段名称
	* @param variable
	*            要修改的变量名称
	* @param value
	*            变量的新值
	* @throws IOException
	*            抛出文件操作可能出现的io异常
	*/
	public static boolean updateValue(String section,String variable, String value) throws IOException {
		String fileContent, line, newLine;
		BufferedReader bufferedReader = new BufferedReader(new FileReader(fileUrl));
		fileContent = "";
		try {
			while ((line = bufferedReader.readLine()) != null) {
				
				line = line.trim();
				if (line.matches(".*=.*") == true) {
					
					int i = line.indexOf('=');
					String key = line.substring(0, i);
					if(key.equals(variable)){
						newLine = key + "=" + value;
						fileContent += newLine + "\r\n";
						while ((line = bufferedReader.readLine()) != null) {
							fileContent += line + "\r\n";
						}
						bufferedReader.close();
						OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(fileUrl,false), "UTF-8");		   
						BufferedWriter bufferedWriter = new BufferedWriter(write);
						bufferedWriter.write(fileContent);
						bufferedWriter.flush();
						bufferedWriter.close();
						System.out.println("修改成功");
						return true;
					}
					
				}			
				fileContent += line + "\r\n";
			}
		} catch (IOException ex) {
			throw ex;
		} finally {
			bufferedReader.close();
		}
		return false;
	}

	/**
	 * 删除指定行
	 * @param section	 要删除的变量所在段名称
	 * @param variable	要删除的变量名称
	 * @return
	 * @throws IOException
	 */
	public static boolean deleteKey(String section,String variable) throws IOException{
		String fileContent, line, newLine;
		BufferedReader bufferedReader = new BufferedReader(new FileReader(fileUrl));
		fileContent = "";
		try {
			while ((line = bufferedReader.readLine()) != null) {
				
				line = line.trim();
				if (line.matches(".*=.*") == true) {
					
					int i = line.indexOf('=');
					String key = line.substring(0, i);
					if(key.equals(variable)){
						newLine = "";
						fileContent += newLine;
						while ((line = bufferedReader.readLine()) != null) {
							fileContent += line + "\r\n";
						}
						bufferedReader.close();
						
						OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(fileUrl,false), "UTF-8");		   
						BufferedWriter bufferedWriter = new BufferedWriter(write);
						bufferedWriter.write(fileContent);
						bufferedWriter.flush();
						bufferedWriter.close();
						System.out.println("删除 成功");
						return true;
					}
					
				}			
				fileContent += line + "\r\n";
			}
		} catch (IOException e) {
			throw e;
		} finally {
			bufferedReader.close();
		}
		return false;
	}
	
	/**
	 * 插入所有内容
	 * @param fileUrl	 配置文件的路径
	 * @param allInfo	所有信息
	 * @return
	 */
	public static boolean addAllValue(String allInfo){
		try{
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(fileUrl,false), "UTF-8");		   
			BufferedWriter bufferedWriter = new BufferedWriter(write);
			bufferedWriter.write(allInfo);
			bufferedWriter.flush();
			bufferedWriter.close();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}

}

 类似资料: