我asked the question the other day并重写代码以反映解决方案,但现在我遇到了一个新问题。这是代码。
这是调用/写入功能;我有8种proizvod
类型,它们生成8个XML文件。创建XML文件后,需要使用自定义zip类压缩:
generateXML(tmpParam,queryRBR,proizvod.getOznaka());
writeToZip(proizvod.getOznaka());
这是writeToZip
:
ZipEntry ze = new ZipEntry(oznaka + ".xml");
FileOutputStream fos = new FileOutputStream(new File(zipFolder + oznaka + ".zip"));
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream(new File(zipFolder + oznaka + ".xml"));
final byte[] buffer = new byte[1024];
int n;
while ((n = fis.read(buffer)) != -1)
zos.write(buffer, 0, n);
zos.closeEntry();
zos.flush();
zos.close();
fis.close();
这是generateXML
:
PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(zipFolder +oznaka + ".xml")));
writer.print("\n<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
writer.print("\n<PROSTORNE_JEDINICE>");
stmt = cm.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String q = "";
rs = stmt.executeQuery(q);
if (rs != null) {
System.out.println("Početak u : " + Util.nowTime());
while (rs.next()) {
writer.print("\n\t<row>");
writer.print("\n\t\t<ID>" + Util.transformToHTML(rs.getInt("id")) + "</ID>");
writer.print("\n\t\t<JED_ID>" + Util.transformToHTML(rs.getInt("jed_id")) + "</JED_ID>");
//etc
writer.print("\n\t</row>");
}
System.out.println("Kraj u : " + Util.nowTime());
}
writer.print("\n</PROSTORNE_JEDINICE>");
generateXML
部分仍占用大量内存(如果我猜错了,它会尽可能地一点一点地进行)并且我看不出如何优化它(使用另一种方式)提供writer.print函数)?
答案 0 :(得分:0)
我立即看到的两件事(也许是复制/粘贴错误)是,你没有关闭generateXml中的 PrintWriter (writer)而你没有关闭 FileOutputStream (fos)在WriteToZipFile中。
为了找到记忆 - 陈词滥调,我建议dotTrace - 也许你可以在你的代码行为中发现一些不正常现象。