【发布时间】:2013-09-18 08:13:56
【问题描述】:
我正在开发一个应用程序,它有一个下载几个文件的启动屏幕,在文件开始下载之前我想检查文件是否已经存在,如果它们存在我想删除它们。下面显示的代码包含正确的文件路径,并且检查文件是否存在的功能似乎可以正常工作,因为 Logcat 中的读出状态为“文件已删除”。
但是,当我检查手机本身时,我发现每当我启动应用程序时,就会有 2 个文件被添加到文件夹中,文件名相同,但数字不断增加
例如启动 1... 我明白了
clientraw.txt
clientrawextra.txt
启动 2... 我明白了
clientraw.txt
clientrawextra.txt
clientraw-1.txt
clientrawextra-1.txt
等等……
因此,删除功能似乎不起作用,我们将不胜感激!
//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();
似乎删除文件的速度不够快?当我摆脱ready();方法(下载文件的方法)时,它确实删除了文件,所以我认为文件在删除之前的文件之前就开始下载真的卡在这个了吗?!
【问题讨论】:
-
Log.d("文件", "文件已删除");在控制台中打印?
-
在日志中打印 file.getAbsolutePath() 并确认文件路径。如果该路径中存在文件,则 file.delete() 必须删除该文件。此外,您可以检查 file.delete() 返回的布尔值。
-
我遇到了同样奇怪的问题。
file.delete()返回 true,但它仍然存在并且可读。
标签: android file download delete-file