简体   繁体   English

用 Zeros Android 覆盖文件

[英]Overwrite File with Zeros Android

我正在尝试用零覆盖一个文件,以便无法恢复该文件。用零覆盖它......任何想法如何做到这一点将不胜感激。

Note: this answer has probably not aged well, and bits of it may be incompatible with Android Q's new file reading/writing model.注意:这个答案可能没有过时,它的一些部分可能与 Android Q 的新文件读/写模型不兼容。 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+ 如果您的目标是 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.这应该能够向目标文件写入大量 0。 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.然后它循环 100 次(如果您愿意,可以删除)并将这些 0 写入文件 100 次。

If you want the user to be able to pick a specific file, see this answer如果您希望用户能够选择特定文件,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM