简体   繁体   English

java从文件夹中删除文件

[英]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! 输出: Success: logfile11052015.log is deleted!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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