0

I've been trying to write new data to a file but it refuses to rename the file, which causes the file to not be overwritten and deleted at the end of my code:

private URL gameHistoryURL = Game.class.getClassLoader().getResource("Files/GameHistory.csv");
private String gameHistoryPath = gameHistoryURL.getPath();

protected void writeToGameHistory(int game) {
    String tempFile = "temp1.txt";
    File oldFile = new File(gameHistoryPath);
    File newFile = new File(tempFile);

    try {
        FileWriter fw = new FileWriter(tempFile);
        FileReader fr = new FileReader(tempFile);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        LineNumberReader count = new LineNumberReader(fr);
        s = new Scanner(new File(gameHistoryPath));

        String gameName;
        int lineNum = count.getLineNumber() + 1;

        //Skip the first line if line number is 10
        if (lineNum >= 10) {
            s.nextLine();
        }

        while (s.hasNext()) {
            String x = s.nextLine();
            pw.println(x);
        }
        switch (game) {
            case 1: {
                pw.println("Game1");
                break;
            }
            case 2: {
                pw.println("Game2");
                break;
            }
            case 3: {
                pw.println("Game3");
                break;
            }
        }
        s.close();
        pw.flush();
        pw.close();
        File f = new File(gameHistoryPath);
        oldFile.delete();
        newFile.renameTo(f);
        System.out.println(newFile + " " + gameHistoryPath);
    }
    catch (Exception e) {
        System.out.println("Error: " + e);
    }
}

The print lines in the try method simply return:

temp1.txt [File Path]/Files/GameHistory.csv

How can I make sure that the temp1.txt file is given the correct directory to overwrite the correct file?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Rotav
  • 75
  • 5
  • 1
    In most cases, this means some other resource has access to the file. Make sure no other resource is using the file then try to rename it. – SedJ601 Nov 26 '18 at 16:51
  • Also how does `File f` do anything if it's pointing to the oldFile which is deleted? Try doing something like `String f = oldFile.getName();` – SedJ601 Nov 26 '18 at 16:55
  • @Sedrick `File f` was used because `newFile.renameTo()` accepts type File. I tried doing `String f = oldFile.getName(); newFile.renameTo(new File(f));` but this yielded the same issue. – Rotav Nov 26 '18 at 17:30
  • Try `String f = oldFile.getName(); oldFile.delete(); newFile.renameTo(new File(f));`. More info [here](https://stackoverflow.com/questions/1158777/rename-a-file-using-java). – SedJ601 Nov 26 '18 at 17:35
  • @Sedrick Already tried this method. – Rotav Nov 26 '18 at 17:47

1 Answers1

0

You open fw as tempFile and while this is open, you can't rename it or delete it on all OSes esp Windows.

I suggest you use try-with-resource and always close a file before attempting to rename or delete it.

BTW new FileWriter(tempFile); truncates the file so it will always be empty if you try to read it.


The purpose of this method appears to be to append a line to the end of the file to record each game as it is played.

protected void writeToGameHistory(int game) {
    // create a new file, or append to the end of an existing one.
    try (PrintWriter pw = new PrintWriter(new FileWriter(gameHistoryPath, true))) {
        pw.println("Game" + game);
        System.out.println(gameHistoryPath + " added Game" + game);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

I don't know if you need to debug line.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks for the help. The code is also meant to delete the first line of the .csv if the row length is equal to or greater than 10, which is why I was trying to overwrite the file rather than just append to the file. Would you still recommend overwriting the file or is there an easier way to achieve this? – Rotav Nov 26 '18 at 17:22
  • 1
    The only way to delete a row is to overwrite the file. – Peter Lawrey Nov 26 '18 at 17:24