当我尝试使用以下内容将一些文本内容设置到我们心爱的窗口的剪贴板时
片段,它可以正常工作到大约 10-15MB。但超出该大小,它会引发错误消息
显示在最后。我们如何将 30+ MB 的 utf 文本设置到剪贴板?
代码:
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
File tmp = new File("c:\\web\\a.txt"); //This file's size > 38MB.
String s = readFileAsString(tmp);
cb.setContents(new StringSelection(s), null);
错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:45)
at java.lang.StringBuffer.<init>(StringBuffer.java:91)
at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1001)
at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
at com.webform.XMLReader.main(XMLReader.java:67)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)