file.delete()方法会将文件删除,但文件所占的空间不会及时释放出来,要想及时释放空间可以先将文件清空再调用delete方法,代码如下:
public static void delete(File file) {
if (file == null || !file.exists() || !file.isFile()) {
return;
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(new byte[1]);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
file.delete();
}