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);
}
4

1 回答 1

2

您正在打开文件以进行覆盖而不是追加。您想将FileWriter初始化更改为:

FileWriter writeFile = new FileWriter(inFile, true);
于 2014-03-04T21:05:28.633 回答