2014-03-04 69 views
1

因此,我正在編寫一個程序,它將接受遊戲名稱,發佈者和價格(作爲字符串)回到已經有一些記錄的文件中。但是,當我運行該程序時,它會擦除​​以前的記錄。輸出流中數據被擦除

 //open the output stream 
     FileWriter writeFile = new FileWriter(inFile); 
     BufferedWriter lineWriter = new BufferedWriter(writeFile); 
     PrintWriter printLine = new PrintWriter(lineWriter); 

if (menuItem == ADD_GAME) 
{ //Get game 
     System.out.print("Enter name of game: "); 
     gameName = Keyboard.readString(); 

     //get publisher 
     System.out.print("Enter name of publisher: "); 
     publisherName = Keyboard.readString(); 

     //get price of game 
     System.out.print("Enter game price: "); 
     gamePrice = Keyboard.readString(); 

     //add game to games 
     createdGame.addGame(gameName, publisherName, gamePrice); 

     //add game to file record 
     outLine = gameName + " " + publisherName + " " + gamePrice; 
     printLine.println(outLine); 
} 

回答

2

您正在打開覆蓋文件而不是附加文件。您想要將FileWriter初始化更改爲:

FileWriter writeFile = new FileWriter(inFile, true);