The delete function does not work

I am developing an application with a screen saver downloading several files before the files start to load. I want to check if the files already exist or not, and if they exist, I want to delete them. The code shown below contains the correct paths to the file, and the file check function seems to work, as reading in Logcat indicates "file deleted."

However, when I check the phone itself, I see that every time I launch the application, another 2 files are added to the folder with the same name, but with an increase in the number

eg. Launch 1 ... I get

clientraw.txt clientrawextra.txt 

Run 2 ... I get

 clientraw.txt clientrawextra.txt clientraw-1.txt clientrawextra-1.txt 

etc.

Therefore, it would seem that the delete function does not work, 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(); 

Doesn't it seem to delete the file fast enough? When I get rid of the ready(); method ready(); (a method that downloads files), it deletes the files perfectly, so I think the files start loading before the previous files are deleted, really stuck on this ?!

+3
source share
5 answers

try it

 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(); } 
+3
source
 File file = new File(selectedFilePath); boolean deleted = file.delete(); 

where selectedFilePath is the path to the file you want to delete, for example:

 /sdcard/YourCustomDirectory/ExampleFile.mp3 

If it does not work, check the path

0
source

This is either because you do not have permissions to

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

or because you have not yet released your handle to this file after downloading. if you opened any file channel or input streams or buffered readers in this file, you must close them before trying to delete the file.

0
source
 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 the Android manifest. You must be authorized to make changes.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
0
source

you can delete a specific file through the 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"); } } 
0
source

All Articles