2

I am trying to overwrite a file with zeros so that the file cannot be restored..I have searched a lot but there is nothing for android ,all what I found was for windows..So I want to choose w file from internal storage then overwrite it with zeros..any idea how to that will be appreciated.

Mrad4Tech
  • 205
  • 2
  • 12

1 Answers1

2

Note: this answer has probably not aged well, and bits of it may be incompatible with Android Q's new file reading/writing model. The general way of writing is still fine, though, but the bits involving file access may require editing before use

Firstly, add this permission to your manifest:

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

Make sure you request it at runtime if you target API 23+

Then, we want to do the writing:

File file = new File(context.getFilesDir(), filename);
String string = "000000000000000000000000000000000";//this can also be a randomly generated string of 0's. You can mix up numbers as well, or it can just be a static string.
FileOutputStream outputStream;

try {
  outputStream = new FileOutputStream(file);
  // You can also open an InputStream before the OutputStream to read the length of the file if you want a more exact match as opposed to a brute-force override
  for(int i = 0; i < 100; i++)//Loop 100 times to write the 0's many times
      outputStream.write(string.getBytes());//And we write all the 0's to the file 100 times
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

This should be able to write a lot of 0's to the targeted file. When the file is opened, the new content written to it will overwrite whatever is currently in it. Then it loops 100 times (can be removed if you want to) and writes these 0's 100 times to the file.

If you want the user to be able to pick a specific file, see this answer

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I tried it,the file size was 1.2mb,then it became 3.22kb,this sounds cool,but when I open the file,it still opens and shows the same content..is that normal?! – Mrad4Tech May 14 '17 at 10:01
  • `but when I open the file,it still opens and shows the same content..is that normal?! ` Are you using a file explorer? In some cases it may not update the content instantly. Try to close and reopen the browser app and check the file again. Once the file is written to, the new content should show up. The most likely error lies in the file explorer – Zoe May 14 '17 at 10:08
  • I solved this by replacing the 000000 with 1010101..is that safe? – Mrad4Tech May 14 '17 at 10:08
  • Now you are using binary code in your file. It isn't dangerous, but try using 0's but when writing, use `outputStream.write(string)` instead of `outputStream.write(string.getBytes())`. I don't really know if it is save, it depends on the combination of the numbers. it may not even do anything, or you may have created a runnable program. You can also try replacing it with letters (the letter `O` for an instance) – Zoe May 14 '17 at 10:20
  • 1
    Okay buddy,I found solution for that..Thanks a lot for your answer,it helped me a lot,Thanks again! – Mrad4Tech May 14 '17 at 10:34