数据在输出流期间被删除

时间:2014-03-04 20:57:03

标签: java file-io data-loss

所以我正在编写一个程序,它将接受游戏名称,发布者和价格(作为字符串)返回到已经包含一些记录的文件中。但是,当我运行程序时,它会删除以前的记录。

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

1 个答案:

答案 0 :(得分:2)

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

FileWriter writeFile = new FileWriter(inFile, true);