我在做一个应用程序中,我不得不删除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.
由于它指出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();
上一篇:什么特性对应于 Java 中的“同步"?应于、特性、quot、Java
下一篇:Android的 - 如何设置button.setOnClickListener一个名为()的方法如何设置、方法、Android、setOnClickListener