简体   繁体   中英

java delete file from folder

I am trying to delete a file: This is the code and I can't see anything wrong

System.out.println("users//"+"user"+i.getId());
            File f=new File("users//"+"user"+i.getId());
            System.out.println("Can READ: "+f.canRead());
            System.out.println("Can WRITE: "+f.canWrite());
            System.out.println("Can EXEC: "+f.canExecute());
            System.out.println("Exists: "+f.exists());
            System.out.println(f.delete());

Yes, I have the right to read,write,exec and the file exists. I don't have any exceptions

I have tried this code and it works:

public static void main(String[] args) {
        try {
            File file = new File("c:\\Users\\Akhil\\logfile11052015.log");
            if (file.delete()) {
                System.out.println("Success: " + file.getName() + " is deleted!");
            } else {
                System.out.println("Failed: Delete operation is failed.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

May be you can check if you are providing correct path in your case and file exists there.

Output: Success: logfile11052015.log is deleted!

删除后,我需要调用System.gc()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM