简体   繁体   中英

How To Append the Large Text Files to JTextArea in java Swing

我已经实现了一个 Java Swing 应用程序。其中我编写了 Open File Functionality。我尝试了很多方法来读取文件并写入 JTextArea(我尝试过使用 append()、setText() 和 read() 方法也)。但是,它最多可以工作 100 MB。如果我想打开超过 100 MB 的文件,它会在 textarea.append() 处引发“内存不足异常:Java 堆空间”。有没有办法将超过 100 MB 的数据附加到JTextArea 或以任何方式增加 JTextArea 的内存容量。请对上述问题提出建议。谢谢。

Possibly a duplicate of Java using up far more memory than allocated with -Xmx as your problem is really that your java-instance is running out of memory. Java can open (theoretically) files of any size, as long as you have the memory for it to be read.

I would however recommend that you only read in parts of a file in memory at a time. And when you've finish with that part you move on to the next specified amount of text.

Anyhow, for this instance and if this is not a regular problem, you could use -Xmx800m which would let java use 800mb for heap space.

If this is not a one time thing, you really should look in to just reading in parts of a file at a time. http://www.baeldung.com/java-read-lines-large-file should put you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM