簡體   English   中英

如何從JAVA文件夾中刪除特定文件,而不刪除文件夾本身?

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

我在刪除文件夾中的特定文件時遇到問題,這就是我想發生的事情:

我想刪除一個文件,其文件名為..

這是我的代碼:

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

誰能幫我解決這個問題。 它實際上並沒有刪除文件。 而且我猜想,那里的delete()沒有任何作用。 非常感謝您的幫助。

你為什么要檢查存在性? 只需嘗試刪除,然后趕上警報。 將錯誤消息放入陷阱中。

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