我正在开发一个应用程序,它有一个启动屏幕,可以下载几个文件,在文件开始下载之前,我想检查文件是否已经存在,如果存在,我想删除它们。下面显示的代码包含正确的文件路径,并且检查文件是否存在的函数似乎与 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();
方法(下载文件的方法)它确实可以很好地删除文件,所以我想在删除以前的文件之前开始下载文件真的卡在这个了吗?!
public static boolean deleteDirectory(File path) {
// TODO Auto-generated method stub
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return(path.delete());
}
这段代码会帮助你..并且在Android Manifest中你必须获得进行修改的权限..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)