我正在从一个目录中获取所有文件,然后根据需要从中挑选文件并将它们存储在一个数组中,现在我想对最后修改的文件进行排序。这是我正在使用的代码
public static int GetFilesCount(File folderPath,int count,String type,Context context)
{
BackupCount=count;
BackupFolderPath=folderPath;
Backuptype=type;
con=context;
DatabaseHandler objhandler;
Cursor cursor=null;
int total = 0;
String ext="";
// Check files count set by user
File[] fList = folderPath.listFiles();
ArrayList<String> myfiles = new ArrayList<String>();
for (File file : fList){
if (file.isFile()){
try {
String FileName=file.getName();
ext=GetFileExtension(FileName);
if(ext.equals("db"))
{
objhandler=new DatabaseHandler(context, folderPath+File.separator+FileName, null);
database= objhandler.openDataBase();
String selectQuery = "SELECT * FROM "+ type + " LIMIT 1";
cursor = database.rawQuery(selectQuery, null);
Integer ColCount=cursor.getColumnCount();
if(cursor.getCount()>0)
{
if(Backuptype.equals("SMS"))
{
if(ColCount.equals(9))
{
myfiles.add(FileName);
total++;
}
}
else if(Backuptype.equals("CallLogs"))
{
if(ColCount.equals(6))
{
myfiles.add(FileName);
total++;
}
}
else if(Backuptype.equals("Contacts"))
{
if(ColCount.equals(9))
{
myfiles.add(FileName);
total++;
}
}
}
if(total>count)
{
// String[] listFiles=new String[myfiles.size()];
// listFiles = myfiles.toArray(listFiles);
// File[] f = null;
for(int i=0;i<=myfiles.size();i++)
{
// f[i]=new File(folderPath+File.separator+myfiles.get(i));
System.out.println("Total SMS Files: "+myfiles.size());
System.out.println("file in folder: "+myfiles.get(i).toString());
}
/*Arrays.sort(f, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
} });*/
System.out.println("file in folder: "+myfiles.size());
// Deletefile(folderPath+File.separator+myfiles.get(0));
}
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
cursor.close();
database.close();
}
}
}
return 1;
}