gpt4 book ai didi

java - 如何使用InputStream "efficiently"逐行读取文本文件?

转载 作者:行者123 更新时间:2023-12-02 05:38:17 28 4
gpt4 key购买 nike

我正在编写一个 Java 小程序,并且正在尝试读取一个 220K 行 (9.2 MB) 的文本文件,该文件是用 .jar 归档的。我相信我对文本文件的唯一合理访问是InputStream。为了使用 InputStream 读取我的文本文件,我推出了自己的逐行阅读器,如下所示。

然而,将InputStream与我自己的逐行读取器一起使用会导致java.lang.OutOfMemoryError:Java堆空间。关于如何读取我试图与我的 .jar 小程序捆绑的文本文件,有什么建议吗?

这是我尝试逐行读取InputStream:

public class InputStreamUtil {
private static final int _CR = 13;
private static final int _LF = 10;
private int _last = -1; // The last char we've read
private int _ch = -1; // currently read char
private InputStream in;

public InputStreamUtil(InputStream i) {
in = i;
}

/**
* Read a line of data from the underlying inputstream
*
* @return a line stripped of line terminators
*/
public String readLine() throws IOException {
StringBuffer sb = new StringBuffer("");
if (_last != -1)
sb.append((char) _last);
_ch = in.read();
while (_ch != _CR && _ch != _LF) {
sb.append((char) _ch);
_ch = in.read();
}
// Read the next byte and check if it's a LF
_last = in.read();
if (_last == _LF) {
_last = -1;
}
return (new String(sb));
}
}

这是小程序控制台的完整错误:

ed reader.
Read file input lines...
Exception caught: java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3326)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:622)
at java.lang.StringBuffer.append(StringBuffer.java:383)
at utilities.InputStreamUtil.readLine(InputStreamUtil.java:28)
at utilities.CensusResultsCalculator.parseCensusData(CensusResultsCalculator.java:216)
at utilities.CensusResultsCalculator.getResultsSequentially(CensusResultsCalculator.java:101)
at logic.PopulationCalculatorVersion1.<init>(PopulationCalculatorVersion1.java:33)
at logic.InteractionHandler.preprocess(InteractionHandler.java:101)
at visualization.USMaps.pqPreprocess(USMaps.java:575)
at visualization.MapPane.update(MapPane.java:328)
at java.util.Observable.notifyObservers(Observable.java:159)
at java.util.Observable.notifyObservers(Observable.java:115)
at visualization.InteractionPane.initMapGrid(InteractionPane.java:233)
at deploy.WebApplet.init(WebApplet.java:206)
at deploy.WebApplet$1.run(WebApplet.java:67)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

I believe the only reasonable access I have to the text file is InputStream.

为什么?

使用 BufferedReader。每秒数百万行。

并且它在您自己的代码中没有错误,特别是您没有在可能出现的多个位置检查流结束。

关于java - 如何使用InputStream "efficiently"逐行读取文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755730/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com