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