0

It seems that its creating a new file always I try to write or read. Each line starts with the name of the player, if exists the player should add the score at the end, if not creates a new line and write the info. .......................

public class JogadorData {

private String nome_player;
private Scanner is;
private FileWriter os;
    // this file exists
private final String path = "src/Data/JogadorData";

public JogadorData(String nome_player) {
    this.nome_player = nome_player;
    try {
        is = new Scanner(new File(path));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } 
    try {
        os = new FileWriter(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void escreverScore(String score) {
    if (jogadorNovo(nome_player)) {
        try {
            os.write(nome_player + " " + score);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else {
        escreverResultadoJogadorExistente(score);
    }

    try {
        is.close();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

    // returns true if it is a new player
    private boolean jogadorNovo(String nome_player) {

    while (is.hasNextLine()) {
        String linha = is.nextLine();
        String[] info = linha.split(" ");

        if (info[0].equals(nome_player)) {
            return false;
        }
    }

    return true;
}
}

.................................... .................................... Test:

 public class TESTE {

public static void main(String[] args) {

    JogadorData jogador = new JogadorData("Manelina");

    jogador.escreverScore("100");

    // System.out.println(jogador.lerMelhorResultado());

}

}
  • You'll also want to become familiar with [The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html), it will save you a lot of issues – MadProgrammer Jul 15 '19 at 00:05
  • I tried that... – Yunrin 127 Jul 15 '19 at 00:09
  • 1
    You tried what? – MadProgrammer Jul 15 '19 at 00:11
  • The info of the content you shared – Yunrin 127 Jul 15 '19 at 00:18
  • `try with resources` won't solve your immediate problem, it will only help you prevent other possible issues. Read the duplicate answer(s) as they will provide you with a number of possible ways to solve your problem. Also take the time to read the [`FileWriter` JavaDocs](https://docs.oracle.com/javase/10/docs/api/java/io/FileWriter.html), as they will provide you with clues. Also, you really don't want to try and open the file for read and writing simultaneously, instead, read or write it as needed in a "atomic" manner – MadProgrammer Jul 15 '19 at 00:24

1 Answers1

0

The example below is a simplified read/write to file from what you have, done in similar format to what you are trying to do. What this code does is reads every line from the file you are loading from via Files#readAllLines, then runs through each line, (put your logic where I commented the if statement, and then output.add appends the new version of the line you are modifying, storing it in the array list "output", after which the file is saved to the path defined by Files#write

List<String> output = new ArrayList<>();
List<String> lines = Files.readAllLines(Paths.get("Path/To/File.txt"));
for (String line : lines) {
    //... if (playerExists(line))
    output.add(line + " " + score);
}
Files.write(Paths.get("Path/To/Save/File.txt"), output);
Joe
  • 1,316
  • 9
  • 17