问题
I am developing an app which has a splash screen that downloads a couple of files, before the files start to download I want to check whether the files already exist or not, and if they exist I want to delete them. The code shown below contains the right file paths and the function to check if the file exists seems to work as the read out in Logcat states "file deleted".
However when I check on the phone itself I see that whenever I launch the app 2 more files are added to the folder with the same file name but with increasing numbers
e.g. Launch 1... I get
clientraw.txt
clientrawextra.txt
Launch 2... I get
clientraw.txt
clientrawextra.txt
clientraw-1.txt
clientrawextra-1.txt
and so on.....
Therefore it would seem the delete function isn't working, any help would be appreciated!
//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();
It seems that it doesn't delete the file fast enough? When I get rid of the ready();
method (The method that downloads the files) it does delete the files fine, so I'm thinking that the files start downloading before the previous files are deleted really stuck on this one?!
回答1:
Try this
void deleteExternalStoragePublicPicture() {
// Create a path where we will place our picture in the user's
// public download directory and delete the file. If external
// storage is not currently mounted this will fail.
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "DemoPicture.jpg");
file.delete();
}
回答2:
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());
}
This Code will Help you.. And In Android Manifest You have to get Permission to make modification..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
回答3:
File file = new File(selectedFilePath);
boolean deleted = file.delete();
where selectedFilePath is the path of the file you want to delete - for example:
/sdcard/YourCustomDirectory/ExampleFile.mp3
If it doesn't works, check the path
回答4:
It's either because you do not have the permissions for
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
or because you have not released your handle on that file yet after your download. if you have opened any File-channel's or input Streams or buffered-readers on that file you should close them before you try to delete the file.
回答5:
you can delete a particular file through file path... Try this
public static void deletFile(String file) {
File f = new File(file);
if (f.delete()) {
Log.d("00000", "delete");
}
else{
Log.d("00000", "Not delete");
}
}
来源:https://stackoverflow.com/questions/18867058/delete-function-not-working