/* 字节形式读取模板文件内容,将结果转为字符串 */ String sourcecontent = ""; InputStream ins = null; try { ins = new FileInputStream(sourceFilePath); byte[] b = new byte[1024]; if (ins == null) { //logger.info(RTFToWordUtil.class.getName()+":源模板文件不存在"); } int bytesRead = 0; while (true) { bytesRead = ins.read(b, 0, 1024); // return final read bytes // counts if (bytesRead == -1) {// end of InputStream //logger.info(RTFToWordUtil.class.getName()+":读取模板文件结束"); break; } // convert to string using bytes sourcecontent += new String(b, 0, bytesRead);
} } catch (Exception e) { e.printStackTrace(); }
/* 修改变化部分 */ String targetcontent = ""; int i = 0; for (String key : map.keySet()) { String value = map.get(key); if (i == 0) { targetcontent = replaceRTF(sourcecontent, key, value); } else { targetcontent = replaceRTF(targetcontent, key, value); } i++; } /* 结果输出保存到文件 */ try { FileWriter fw = new FileWriter(targetFilePath, true); PrintWriter out = new PrintWriter(fw);