简体   繁体   English

如何从JAVA文件夹中删除特定文件,而不删除文件夹本身?

[英]How to delete specific file from folder in JAVA, without deleting the folder itself?

I'm having a problem in deleting a specific file in a folder, This is what I want to happen: 我在删除文件夹中的特定文件时遇到问题,这就是我想发生的事情:

I want to delete a file, having the file name of.. 我想删除一个文件,其文件名为..

This is my code by the way: 这是我的代码:

try
{
    File f=new File("C://Generated Barcodes//"+file_copy.getText()+".png");//full path like c:/home/ri
    if(!f.exists())
    {
        JOptionPane.showMessageDialog(null, "Something Went Wrong!",
        " ", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
        f.delete();
    }
} catch(Exception e) {
    e.printStackTrace();
}

can anyone help me out on how to do it.? 谁能帮我解决这个问题。 It did not delete the file actually. 它实际上并没有删除文件。 And I guess, the delete() there, doesn't do anything. 而且我猜想,那里的delete()没有任何作用。 Your help is highly appreciated. 非常感谢您的帮助。

Why do you check for existence? 你为什么要检查存在性? Just try deleting, and catch with the alert. 只需尝试删除,然后赶上警报。 Put the error message in the catch. 将错误消息放入陷阱中。

try
{
    File f=new File("C:/Generated Barcodes/" + file_copy.getText() + ".png");
    boolean deleted = f.delete();
} catch(Exception e) {
    JOptionPane.showMessageDialog(null, "Something Went Wrong!",
        " ", JOptionPane.ERROR_MESSAGE);
}

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

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