Java tutorial
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Main { public static boolean reloadMapperXml(String spath, String tPath, String name) { try { String linetext = ""; String resultMapText = ""; StringBuffer sbf = new StringBuffer(); File f = new File(spath + name); BufferedReader br = new BufferedReader(new FileReader(f)); int lineNum = 1; while ((linetext = br.readLine()) != null) { sbf.append(linetext); sbf.append("\r\n"); if (lineNum == 4) { resultMapText = linetext; } lineNum++; } br.close(); String text = sbf.toString(); if (text.contains("queryPage")) { return false; } int si = text.indexOf("<insert id=\"insert\""); int ei = text.indexOf("</insert>"); String noinsert = text.substring(si, ei + 9); text = text.replace(noinsert, ""); int su = text.indexOf("<update id=\"updateByPrimaryKey\""); int eu = text.indexOf("</update>", su); if (su > 0 && eu > 0) { String noupdate = text.substring(su, eu + 9); text = text.replace(noupdate, ""); } text = text.replace("selectByPrimaryKey", "selectById"); text = text.replace("deleteByPrimaryKey", "deleteById"); text = text.replace("insertSelective", "insert"); text = text.replace("updateByPrimaryKeySelective", "updateById"); int sp = text.indexOf("<select"); int ep = text.indexOf("</select>"); String pageSql = ""; if (sp > 0 && ep > 0) { pageSql = text.substring(sp, ep + 9); } pageSql = pageSql.replace("selectById", "queryPage"); pageSql = pageSql.substring(0, pageSql.indexOf("where")); pageSql = pageSql + "</select>"; int sc = resultMapText.indexOf("type="); if (sc > 0) { resultMapText = resultMapText.substring(sc + 6); } int ec = resultMapText.lastIndexOf("\""); if (ec > 0) { resultMapText = resultMapText.substring(0, ec); } pageSql = pageSql.replace("java.lang.Integer", resultMapText); text = text.replace("</mapper>", ""); StringBuffer newSql = new StringBuffer(text); newSql.append(pageSql); newSql.append("</mapper>"); saveFile(tPath, name, newSql.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } public static void saveFile(String tPath, String name, String text) { try { File file = new File(tPath); file.mkdirs(); FileWriter fw = new FileWriter(tPath + name, false); fw.write(text); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }