2016-01-17 156 views
0

我想在某人贏了或者輸了一個二十一點遊戲後重寫一個文件。我每次運行遊戲並獲得勝利,我得到這個錯誤:內存不足錯誤

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個字節。

+0

可能重複[什麼是OutOfMemoryError,以及如何調試和修復它](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug - 修復它) – Raedwald

回答

3

你有一個循環

while(line != null) { 
    ... 
} 

但在循環中,你從來不看另一行。循環從不終止,並且您繼續將相同的數據附加到newFile。最終你會消耗所有的內存。

+0

Brb kms ...多麼愚蠢的錯誤。我會嘗試添加一條新的readline語句 –

0

您賦值給變量line只有一次,在申報階段:String line = IO.readLine();while循環之前sin'ceit發生,內環路您百達使用相同的第一line沒有從輸入文件中的新行重新分配它。

換句話說,while循環會無限地運行,或者直到內存用完爲止,就像您的情況一樣。

您必須在while循環範圍結束之前添加line = IO.readLine();