• Ini配置文件操作


    package cn.com.szhtkj.util;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class IniUtil {
    	static Map<String, Map<String, Object>> iniFile = new HashMap<String, Map<String, Object>>();
    	 
    	 
    	public static void write(String name) throws IOException {
    		StringBuilder sb = new StringBuilder("");
    		for (String section : iniFile.keySet()) {
    			sb.append("[").append(section).append("]").append("\n");
    			Map<String, Object> map = iniFile.get(section);
    			Set<String> keySet = map.keySet();
    			for (String key : keySet) {
    				sb.append(key).append("=").append(map.get(key)).append("\n");
    			}
    		}
    		File file = new File(name);
    		if (!file.getParentFile().exists()) {
    			file.getParentFile().mkdirs();
    		}
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    		try {
    			OutputStream os = new FileOutputStream(file);
    			os.write(sb.toString().getBytes());
    			os.flush();
    			os.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public static  void setValue(String section, String key, Object value) {
    		Map<String, Object> sectionMap = iniFile.get(section);
    		if (sectionMap == null) {
    			sectionMap = new HashMap<String, Object>();
    			iniFile.put(section, sectionMap);
    		}
    		sectionMap.put(key, value);
    	}
     
    	public static  Object getValue(String section, String key) {
    		Object obj = null;
    		Map<String, Object> item = iniFile.get(section);
    		if (item != null) {
    			obj = item.get(key);
    		}
    		return obj;
     
    	}
    	
    	
    	public static void main(String[] args) {
    		setValue("ip", "ip_address", "183");
    		setValue("ip", "ip_address1", "1831");
    		try {
    			write("D:\\UploadFiles\\NET.ini");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    }
    
  • 相关阅读:
    A16 React+AntDesign AntDesignUI框架初识
    A15 React+AntDesign 路由模块化 嵌套路由父子组件传值
    A14 React+AntDesign 路由嵌套
    react中实现JavaScript跳转路由
    react打包 npm run build 出现空白页面
    在react中解析html标签代码
    es6 模板字符串 在字符串里写变量
    A13 React+AntDesign 路由配置 react-router5.1.2
    nginx动态配置,环境安装
    Spring配置文件内容加密
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10955322.html
Copyright © 2020-2023  润新知