统计目录大小和时间删除早期的大文件
来源:未知 责任编辑:智问网络 发表时间:2013-11-12 17:34 点击:次
为什么要做这些,等你需要的时候你就知道了
private void deleteOldestFile(File directory)
{
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}});
files[0].delete();
}
private static long dirSize(File dir) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
if(fileList[i].isDirectory()) {
result += dirSize(fileList [i]);
} else {
// Sum the file size in bytes
result += fileList[i].length();
}
}
return result;
}
File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
Long directorySize = dirSize(dirlist);
if (directorySize > 1073741824) // this is 1GB in bytes
{
deleteOldestFile(dirlist);
}
File TempFile = new File(Environment.getExternalStorageDirectory()
+ "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
或者
while (directorySize > 1073741824)
{
deleteOldestFile(dirlist);
direcotrySize = dirSize(dirlist);
}
最新推荐更多>>>
- 发表评论
-
- 最新评论 进入详细评论页>>