删除功能不起作用

use*_*360 5 android file download delete-file

我正在开发一个带有启动屏幕的应用程序,该屏幕下载了几个文件,在开始下载文件之前,我想检查文件是否已经存在,如果存在,我要删除它们。下面显示的代码包含正确的文件路径,并且由于Logcat中显示的“文件已删除”状态,该功能似乎可以检查文件是否存在。

但是,当我检查手机本身时,我发现每当我启动该应用程序2时,就会以相同的文件名但数量增加的方式将更多文件添加到该文件夹​​中

例如启动1 ...我得到

clientraw.txt
clientrawextra.txt
Run Code Online (Sandbox Code Playgroud)

启动2 ...我明白了

clientraw.txt
clientrawextra.txt
clientraw-1.txt
clientrawextra-1.txt
Run Code Online (Sandbox Code Playgroud)

等等.....

因此,似乎删除功能无法正常工作,我们将不胜感激!

//Code that checks if the clientraw.txt file already exists, if so the file is deleted 
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() +
        "/Download", client);
Log.d("file path", String.valueOf(file));
if (file.exists()) {
    file.delete();
    Log.d("file", "file deleted");
}

File sdCardextra = Environment.getExternalStorageDirectory();
File fileextra = new File(sdCardextra.getAbsolutePath() +
        "/Download", clientextra);
if (fileextra.exists()) {
    fileextra.delete();
    Log.d("file", "file deleted");
}

ready();
Run Code Online (Sandbox Code Playgroud)

似乎删除文件的速度不够快?当我摆脱该ready();方法(下载文件的方法)时,确实可以删除文件,因此我认为在删除以前的文件之前就开始下载文件,这真的卡在了这个文件上吗?

Pri*_*lly 0

要么是因为你没有权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

或者因为您在下载后尚未释放该文件的句柄。如果您已打开该文件上的任何文件通道或输入流或缓冲读取器,则应在尝试删除该文件之前将其关闭。