简体   繁体   中英

How do I stop my file being overwritten each time I Add to the file?

I am trying to make a list of players to add to a file, so I can store them and recall them when needed.

I have managed to write to the file, but each time I re-write to the file the last file entry gets overridden. Has anyone got any idea how I can just save each entry to the file without it being overridden each time?

Any help would get greatly appreciated

Thanks in advance!!!!!!


package p;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Runner {

    public static final Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter("output.txt");
        PlayerData player = null;
        List<PlayerData> players = new ArrayList<>();

        System.out.println("How many players do you want to register? : ");
        int num = input.nextInt();

        while (true) {

            System.out.println("Plz enter Name : ");
            String name = input.next();
            writer.write("Name: " + name + System.lineSeparator());

            System.out.println("Plz enter age : ");
            String age = input.next();
            writer.write("Age: " + age + System.lineSeparator());

            System.out.println("Plz enter Player_id : ");
            String player_id = input.next();
            writer.write("Player_id: " + player_id + System.lineSeparator());

            System.out.println("Plz enter agent_id : ");
            String agent_id = input.next();
            writer.write("Agent_id:" + agent_id + System.lineSeparator());

            System.out.println("Plz enter status : ");
            String status = input.next();
            writer.write("Status: " + status + System.lineSeparator());

            System.out.println("Plz enter position : ");
            String position = input.next();
            writer.write("Position: " + position + System.lineSeparator());

            System.out.println("Plz enter valuation : ");
            Double valuation = input.nextDouble();
            writer.write("Value: " + valuation + System.lineSeparator());

            System.out.println("\n");
            player = new PlayerData(name, age, player_id, agent_id, valuation, status, position);
            players.add(player);

            System.out.println("Information Entered: \n" + player + "\n" + "Name : " + name + "\n" + "Age: " + age
                    + "Player id: " + player_id + "\n" + "Agent id: " + agent_id + "\n" + "Player Value: " + valuation
                    + "\n" + "Player Status:" + status + "\n" + "Player position:" + position + "\n");

            writer.close();
            if (players.size() == num)

                break;
        }

        System.out.println(players);

    }

}


FileWriter writer = new FileWriter("output.txt", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM