I want to delete a file and rename a new file to that old file

Forgive me if this has been asked before, but I’m trying to write some line from an old file into a temp file, then rename the temp file as the old file before deleting the old file. Below is some code I’ve tried, with the usual File .delete() and .renameTo(), plus creating a new public static void Delete Function but it always ends up creating the temp file without deleting or renaming.

        String lectid = jLabel1.getText().trim();
        String passw = Lpass.getText().trim();
        String name = LName.getText().trim();
        String email = Lemail.getText().trim();
        //trim will make sure spaces are ignored
        //File temp = new File("lectemp.txt");
        try {
            Scanner sc = new Scanner(new File("lecturer.txt")).useDelimiter(",");
            //FileWriter writer = new FileWriter("lectemp.dat", true);
            FileWriter writer = new FileWriter("lectemp.txt", true);
            BufferedWriter print = new BufferedWriter(writer);
            sc.nextLine();
            while (sc.hasNextLine()) {
                String swap = sc.nextLine(); //putting it before the while statement causes it to crash
                if(swap.contains(lectid + ",")){
                    print.newLine();
                    print.write(lectid + "," +name+ "," +passw+ "," +email);

                }else{
                    print.newLine();
                    print.write(swap);
                }               
            }
            sc.close();
            print.close();
            writer.close();
            File old = new File("lecturer.txt");
            if(old.delete()){
                System.out.println("deleted");
            }else{
                System.out.println("not deleted");
            }
File old = new File("lecturer.txt");
File temp = new File("lectemp.txt");
old.delete();
temp.renameTo(old);
        String lectid = jLabel1.getText().trim();
        String passw = Lpass.getText().trim();
        String name = LName.getText().trim();
        String email = Lemail.getText().trim();
        //trim will make sure spaces are ignored
        File old = new File("lecturer.txt");
        File temp = new File("lectemp.txt");
        try {
            Scanner sc = new Scanner(new File("lecturer.txt")).useDelimiter(",");
            //FileWriter writer = new FileWriter("lectemp.dat", true);
            FileWriter writer = new FileWriter("lectemp.txt", true);
            BufferedWriter print = new BufferedWriter(writer);
            sc.nextLine();
            while (sc.hasNextLine()) {
                String swap = sc.nextLine(); //putting it before the while statement causes it to crash
                if(swap.contains(lectid + ",")){
                    print.newLine();
                    print.write(lectid + "," +name+ "," +passw+ "," +email);

                }else{
                    print.newLine();
                    print.write(swap);
                }               
            }
            sc.close();
            print.close();
            writer.close();
            Deletion(old,temp);

public static void Deletion(File file1, File file2){ //temp renaming with method
        if(file1.delete()){
                System.out.println("File deleted");
            }else{System.err.println("ERROR");}

            if(file2.renameTo(file1)){
                System.out.println("File renamed");
            }else{System.err.println("ERROR");}
    }

I expect the output to be “lecturer.txt” being deleted, and “lectemp.txt” being renamed to “lecturer.txt” However, it always ends up with both with nothing deleted or renamed. When I try the first code it always says not deleted.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

There are times that the file can fail to delete, for example if you don’t have permission to the file or the file is still open.
As far as I am aware the OS itself won’t be preventing you from deleting the file.

I would recommend changing you code from using

File.delete();

to

try {
    Files.delete(File.deleteToPath);
} catch (Exception e)
    System.out.println("Failed because: " + e.getMessage());
}

Unlike the ‘File’ call the ‘Files’ operation will throw an useful exception so you can actually know why the deletion failed.
If your delete call fails every time this should make it easy to debug.

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#delete(java.nio.file.Path)