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.
Asked
Active
Viewed 446 times
2
-
What is the file extension? – Nirel May 13 '17 at 19:43
-
What is so difficult in writing some zeros to a FileOutputStream? – greenapps May 13 '17 at 19:46
-
@Nirel any file,what ever it is!! – Mrad4Tech May 13 '17 at 19:47
-
That will be a waste of time, as the zeros you write will not necessarily overwrite previous bits, due to the way flash storage works. – CommonsWare May 13 '17 at 19:47
-
@greenapps if so,tell us please – Mrad4Tech May 13 '17 at 19:48
-
What do you find difficult on it? Ever wrote data to a file? – greenapps May 13 '17 at 19:49
-
It is very easy to write zeros to a file output stream. And you will not as much overwrite the bytes off the file as the bytes of the medium. – greenapps May 13 '17 at 19:55
-
Please show how you open a file and write data to it. Then i will show you how to write zeros. – greenapps May 13 '17 at 19:56
1 Answers
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
-
-
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
-
1Okay 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