java从文件夹中删除文件

时间:2015-05-11 13:50:37

标签: java file

我正在尝试删除文件: 这是代码,我看不出任何错误

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());

是的,我有权阅读,写作,执行和文件存在。我没有任何例外

2 个答案:

答案 0 :(得分:3)

我已经尝试过这段代码并且有效:

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();
        }
    }

可能您可以检查您的案例中是否提供了正确的路径,并且文件存在于那里。

输出:Success: logfile11052015.log is deleted!

答案 1 :(得分:-1)

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