0

I am trying to rename the text file "tempa.txt" to "VaccineSupply.txt" using renameTo() at first, but failed, did some shallow research and adopted the method File.move(), it has failed renaming the file too. Below is the code I am trying to implement, it's a method used to delete a row in a text file.

public boolean DelVacSupply(int Target) throws IOException
{
    boolean success = false;
    File F = new File("VaccineSupply.txt");
    FileReader fr = new FileReader(F);
    BufferedReader br = new BufferedReader(fr);
    File Ftc1 = new File("Tempa.txt");
    PrintWriter pr = new PrintWriter(Ftc1);
    String abc = Integer.toString(Target);
    try
    {
        String line = br.readLine();
        while (line!=null)
        {
            String[] wordsinLine = line.split(","); 
            if (wordsinLine[0].equals(abc))
            {
                success = true;
            }
            else
            {
                pr.println(line);
            }

            line = br.readLine();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        pr.flush();
        br.close();
        pr.close();
        fr.close();
        Path source = Paths.get("Tempa.txt");
        Files.move(source, source.resolveSibling("VaccineSupply.txt"),
        StandardCopyOption.REPLACE_EXISTING);
        /*File dump = new File("VaccineSupply.txt");
        F.delete();
        Ftc1.renameTo(dump);*/
    }
    return success;
    
}

The file "tempa.txt" is always there and has not been renamed to "VaccineSupply.txt", the old file "VaccineSupply.txt" is not deleted. It sometimes work but that is a very rare occasion, please help me, thank you.

I am using Windows 10

Syramid
  • 73
  • 1
  • 7
  • 1
    Is there an Exception thrown inside your `finally` block ? – vincrichaud Nov 30 '21 at 16:33
  • @VadymVL When I open the project file, I can see both the text files there, one is "tempa.txt", the file with the deleted row; and the other file is the old file "VaccineSupply.txt" which still retains all the previous data. – Syramid Nov 30 '21 at 16:33
  • @vincrichaud Unfortunately, no :( – Syramid Nov 30 '21 at 16:36

0 Answers0