package com.oumasoft.bstmanage.ibatis.dao; /** * 描述:数据库初始化基本类 * * @作者 王群 * @创建日期 2010-04-08 * @修改人 xxx * @修改日期 xxx * @检查人 xxx * @检查日期 xxx */ import java.sql.SQLException; import com.ibatis.sqlmap.client.SqlMapClient; import com.oumasoft.bstmanage.ibatis.SqlMapConfig; import com.oumasoft.bstmanage.ibatis.data.JsgnPo; import com.oumasoft.bstmanage.ibatis.data.Test; import java.util.*; import org.w3c.dom.*; import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.xml.transform.stream.*; import org.w3c.dom.*; import javax.xml.transform.*; import javax.xml.parsers.*; import javax.xml.transform.dom.*; import org.apache.log4j.Logger; import com.oumasoft.bstmanage.ibatis.dao.ClientDao; public class InitDBDao{ static Logger logger = Logger.getLogger(ClientDao.class.getName()); static SqlMapClient sqlMap = null; private static File file = null;//读写文件 private static DocumentBuilderFactory factory = null; private static DocumentBuilder builder = null; /** * 修改properties文件,参数是map键值集合,根据键修改值,如果没有键值对就创建 * @param filePath 文件路径 * @param keyVal 内容参数 * @param over是否覆盖,true的话就覆盖,false的话就是更新 * @return */ public static boolean writeProperties(String filePath, Map<String, String> keyVal ,boolean over) { boolean flag = false; Properties props = new Properties(); InputStream ips = null; try { ips = new BufferedInputStream(new FileInputStream(filePath)); props.load(ips); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { ips.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("输入关闭出错"); } } OutputStream fos = null; if (over) { // 将新添加的内容覆盖原来的内容 try { fos = new FileOutputStream(filePath); // 将keys转换成数组 // 将键值逐一添加到property中 for (Object str : keyVal.keySet().toArray()) { props.setProperty(str.toString(), keyVal.get(str)); } props.store(fos, "set"); flag = true; } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("输出流无法关闭"); } } }else{ try { fos = new FileOutputStream(filePath); for (Object key : keyVal.keySet().toArray()) { props.setProperty(key.toString(), keyVal.get(key).toString()); } props.store(fos,""); flag = true; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("输出流关闭出错"); } } } return flag; } /** * properties文件按键得值 * * @param filePath properties 文件的路径 * @param key 要取值的键 * @return 得到的值 */ public static String readValue(String filePath, String key) { Properties props = new Properties(); InputStream ips = null; try { ips = new BufferedInputStream(new FileInputStream(filePath)); props.load(ips); String value = props.getProperty(key); return value; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if(null != ips){ ips.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
properties文件读写自己写的方法
最新推荐文章于 2019-06-05 15:41:54 发布