0

I'm working on a project that deals with files and I had difficult time trying to delete a file. Luckily, it was working and it still works for me.

The problem I had was I was trying to delete a file that is empty:

file.length()==0

So I solved my problem but I'm still left with a question.

file.length() returns 0 in both cases

  1. The size is actually zero
  2. The file does not exist in the path

So How am i supposed to eliminate the files that have 0 size in my file explorer. I have too many files with size 0 and I want to erase them from my /files/.

Yes, I tried selecting on the file and clicking on red X several times, but I guess eclipse does not read file with 0 size. With AVD, I could make a new AVD and launch it with formatted setting, but in real life, you can't just buy a new phone to get rid of files with size 0.

Help me how to do this on ECLIPSE please

Thank you

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
user2377897
  • 7
  • 2
  • 5

3 Answers3

0

You can distinguish between a file that does not exist and an empty file by using

if (file.exists() && file.length() == 0) {
    // existing, empty file
} else {
    // file does not exist or exists and is not empty
}

You might also find the methods File.isFile() and File.isDirectory() useful.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

In your code you should first create a file object and then check if it exists before trying to delete it:

File myFile = new File(path); // path is the absolute path of the file that you want to delete
if (myFile.exists()){   
    // Delete
    boolean deletedFile = myFile.delete();
    // Check if the file was deleted
    if(deletedFile){
        // Delete successful                
    }
    else{
        // Error                
    }
}
Julian Mancera
  • 194
  • 1
  • 12
0

Start adb consol and write follwing command

adb shell su $rm /yourpath/to/yourfilename

delete your o size file.

Hemantvc
  • 2,111
  • 3
  • 30
  • 42