我想在某人贏了或者輸了一個二十一點遊戲後重寫一個文件。我每次運行遊戲並獲得勝利,我得到這個錯誤:內存不足錯誤
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at User.setWinnings(User.java:278)
at Blackjack.addWinnings(Blackjack.java:182)
at Blackjack.showWinMessage(Blackjack.java:323)
at Blackjack.showOutcome(Blackjack.java:308)
at Blackjack.actionPerformed(Blackjack.java:401)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
這是setWinnings方法
public void setWinnings(int winnings2) {
winnings = winnings2;
IO.openInputFile("users.txt");
String line = IO.readLine();
StringBuilder newFile = new StringBuilder();
while(line != null){
String tokens[] = line.split("%");
if(tokens.length > 0){
if(tokens[0].equalsIgnoreCase(this.getUserID())){
String newLine = tokens[0] + tokens[1]+ tokens[2] + winnings2 + tokens[4]+ tokens[5]+ tokens[6];
newFile.append(newLine);
newFile.append("\n");
} else {
newFile.append(line);
newFile.append("\n");
}
}
}
IO.closeInputFile();
IO.createOutputFile("users.txt");
IO.println(newFile.toString());
IO.closeOutputFile();
}
我已經嘗試進入控制面板,並改變任何該字段代碼被稱爲java下的-Xms2048m什麼的。任何想法爲什麼這不起作用?
編輯:請注意,users.txt的大小隻有82個字節。
可能重複[什麼是OutOfMemoryError,以及如何調試和修復它](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug - 修復它) – Raedwald