上次做的项目中包括文件擦除部分,一般删除的文件可以被恢复,而擦除过的文件因为写入了一些其他的数据,所以无法被恢复。
下面请看带代码:
Clear.java
package com.huangcheng.smash;
import java.io.File;
public class Clear {
public static void main(String[] args) {
SmashFile s=new SmashFile();
File file=new File("HTEDFS.txt");
s.smashing(file);
file.delete();
}
}
文件擦除类,SmashFile.java
package com.huangcheng.smash;
import java.io.*;
import java.util.*;
/**
*
* @author Administrator
*/
public class SmashFile {
/** Creates a new instance of SmashFile */
private Random rData = new Random();
private DataOutputStream smash;
private byte[] data;
private long temp;
private static boolean flag;
protected boolean getFlag() {
return flag;
}
protected void smashing(File sFile) {
temp = sFile.length();
data = new byte[1024];
flag = sFile.canWrite();
if (flag == true) {
try {
smash = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(sFile)));
smash.writeByte(0);
smash = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(sFile, false)));
for (int i = 1; i <= temp / 1024; i++) {
rData.nextBytes(data);
smash.write(data);
}
smash.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
sFile.deleteOnExit();
}
}
}
希望对大家有帮助!