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