我有一个Java应用程序。此应用程序的可执行jar还包含一些zip和文本文件,它们在应用程序启动时读取。我可以很容易地处理阅读文本文件使用在java jar文件中读取zip文件
getResourceAsStream
,但问题是阅读zip文件。
我试着用下面的代码,但是这只是增加了4次内存使用量。
// location of the file
InputStream is = ChemicalSynonyms.class.getClassLoader().getResourceAsStream(strFileName);
ZipInputStream zis = new ZipInputStream(is);
ZipEntry ze = zis.getNextEntry();
Scanner sc = new Scanner(zis);
String[] wordsArray;
while (sc.hasNextLine())
{
// split on tab and use only the first column
wordsArray = sc.nextLine().toLowerCase().split("\t");
termSet.add(wordsArray[0]);
}
sc.close();
zis.close();
is.close();
如何有效地读取压缩文件,该压缩文件在同一个jar文件中。
****编辑**** 看来问题在于sc.nextLine()。toLowerCase()。split(“\ t”);我发现了几个论坛,他们提到分裂会导致消耗大量内存。
您正在处理的zip文件有多大?这个过程将膨胀,并将所有“单词”放入内存中的数组中。如果你正在处理大量的数据,我不确定是否存在咀嚼记忆的问题。 – slambeth
压缩文件相对较小。总共31MB(压缩后) – user1631306
我怀疑我没有使用正确的方法读取使用扫描仪的zip文件。有没有更好的方法? – user1631306