0

I have a problem with the last 2 lines of code in this function because the file file.txt is still open and is not deleted and tmpFile.txt does not change the name. Copying from file.txt to tmpFile.txt works great. I'm asking for help

public static void transfer(Client client) throws FileNotFoundException, IOException{
        File file = new File("file.txt");
        File tmpFile = new File("tmpFile.txt");

        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));

        try{
            String lineToRemove = client.id + ";" + client.pin + ";" + 
                    client.money + ";" + client.name + ";";
            String currentLine;

            while((currentLine = reader.readLine()) != null) {
                String trimmedLine = currentLine.trim();
                if(trimmedLine.equals(lineToRemove)) continue;
                writer.write(currentLine + "\n");
            }
        }
        finally{
            writer.close();
            reader.close();
        }

        file.delete();
        tmpFile.renameTo(file);

        /*File oldFile = new File("tmpFile.txt");
        File newFile = new File(oldFile.getParent(), "file.txt");
        Files.move(oldFile.toPath(), newFile.toPath());*/
    }
Oblivion
  • 6,731
  • 2
  • 12
  • 32
  • When you say problem, what is the problem you are having ? An exception ? Your code works fine though – TechFree Jun 07 '19 at 17:41
  • 1
    `file.delete()` returns a boolean telling you whether the file was deleted (true) or not (false) — have you checked the return value? I prefer `Files.delete(java.nio.file.Path)` because it throws an IOException telling you why the delete failed. – Stephen P Jun 07 '19 at 17:46

2 Answers2

1

If I run your code without the Client stuff, It works as expected.

The reason you still see your file.txt open is because that is NOT your initial file.txt. It's the renamed tmpFile.txt that now is called file.txt.

With the code below you are left with a file that was renamed from tmpFile.txt to file.txt and it containes "HALLO\n". The initial file file.txt is in fact removed, and doesn`t exist anymore. - thats expected behaviour.

public static void main(String[] args) throws Exception {

        File file = new File("src/file.txt");
        File tmpFile = new File("src/tmpFile.txt");

        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));

        try{
                writer.write("HALLO" + "\n");
        }
        finally {
            writer.close();
            reader.close();
        }

        file.delete();
        tmpFile.renameTo(file);

    /*File oldFile = new File("tmpFile.txt");
    File newFile = new File(oldFile.getParent(), "file.txt");
    Files.move(oldFile.toPath(), newFile.toPath());*/
}
DigitalJedi
  • 1,502
  • 1
  • 5
  • 26
0

Unable to reproduce. I ran your code and it replaced the file.

But, try upgrading the code to use the newer NIO.2 methods:

public static void transfer(Client client) throws IOException {
    Path file = Paths.get("file.txt");
    Path tmpFile = Paths.get("tmpFile.txt");

    try (BufferedReader reader = Files.newBufferedReader(file);
         BufferedWriter writer = Files.newBufferedWriter(tmpFile)) {

        String lineToRemove = client.id + ";" + client.pin + ";" + 
                client.money + ";" + client.name + ";";
        for (String currentLine; (currentLine = reader.readLine()) != null; ) {
            if (! currentLine.trim().equals(lineToRemove))
                writer.write(currentLine + "\n");
        }
    }

    Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
}
Andreas
  • 147,606
  • 10
  • 133
  • 220