Get last Modified Date of Files in a directory

前端 未结 4 2030
Happy的楠姐
Happy的楠姐 2021-01-17 13:38

I am having some trouble displaying the files currently modified date.

public class MyAdapter extends ArrayAdapter {

String dir = \"/FileDire         


        
4条回答
  •  终归单人心
    2021-01-17 13:50

    In your question, you are pointing a Directory, not a File.

    File file = new File(Environment.getExternalStorageDirectory() + dir);
    
    
    private final Activity context;
    
    Date lastModified = new Date(file.lastModified()); 
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
    String formattedDateString = formatter.format(lastModified);
    

    The idea is to get a Directory and iterate searching for the Last modified date of all files. The following question may help: How to get only 10 last modified files from directory using Java?

    EDIT 1:

    My Tricky Solution:

    File images = new File("YourDirectoryPath");
    long[] fileModifieDate = new long[images.listFiles().length];
    int i=0;
    
            File[] imagelist = images.listFiles(new FilenameFilter()
            {
    
                public boolean accept(File dir, String name)
                {
                    File file = new File(dir, name);
                    fileModifieDate[i++] = file.lastModified();
                    return true;
                }
    
            });
    // Here, max is the last modified date for this directory 
    // Here, Long array **fileModifieDate** will give modified time of all files, which you can also access from Files array
    // if you want the last modified file in the directory you can do this:
    
    
            File[] maxModifiedDate = images.listFiles(new FilenameFilter()
            {
    
                public boolean accept(File dir, String name)
                {
                    File file = new File(dir, name);
    
                    return file.lastModified() == max;
                }
    
            });
    
    // Now **maxModifiedDate** File array will have only one File, which will have max modified date.
    

    EDIT 2:

    For your case, this would be helpful:

    public class MyAdapter extends ArrayAdapter {
    
    String dir = "/FileDirectory/";
    
    File myFolder= new File(Environment.getExternalStorageDirectory() + dir);
    if(myFolder.exists()){
    
    
        File[] filelist = myFolder.listFiles(new FilenameFilter()
        {
    
            public boolean accept(File dir, String name)
            {  
                return true;
            }
    
        }); 
    }   
    

    // Now you have a filelist array of Files. If you want lastModified data, you can fetch from each individual file as you were doing previously:

      private final Activity context;
    
        Date lastModified = new Date(file.lastModified()); 
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
        String formattedDateString = formatter.format(lastModified); 
    
        private String dateFormat = "dd/MM/yyyy HH:mm:ss";
    

提交回复
热议问题