删除文件从SD卡中的机器人 [英] Deleting file from sdcard in android
问题描述
我在做一个应用程序中,我不得不删除SD卡最近添加的mp3文件。其中歌曲保存的格式为:
I am making an app in which I have to delete the recently added mp3 file in sdcard. The format in which the song is saved is:
Songhello_17_26.amr
其中 17_26 是当歌曲被添加的时间。谁能帮助我如何删除SD卡最近添加的文件。我的意思是说,我想要删除的时间意味着最新添加的文件应该被删除。 任何帮助将AP preciated。
where 17_26 is the time when song was added. Can anyone help me how delete the recently added file in sdcard. I mean to say that I want to delete the time means the latest added file should get deleted. Any help will be appreciated.
推荐答案
由于它指出<一href="http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified">here,你不能做到这一点直接,你首先需要获得的文件列表 File.listFiles()
,比较
, File.lastModified()
, Arrays.sort()
删除。
As it states here, you cant do that directly, you first need to get list of files File.listFiles()
, Comparator
,File.lastModified()
, Arrays.sort()
and delete.
编辑:
File f = new File(path);
File [] files = f.listFiles();
Arrays.sort( files, new Comparator()
{
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
要删除最近的一次:
files[0].delete();
这篇关于删除文件从SD卡中的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!