获取目录中文件的上次修改日期

时间:2013-09-14 16:09:30

标签: java android file

我在显示当前修改日期的文件时遇到了一些问题。

public class MyAdapter extends ArrayAdapter<String> {

String dir = "/FileDirectory/";

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); 

private String dateFormat = "dd/MM/yyyy HH:mm:ss";

static class ViewHolder {

    public TextView text;
}

public MyAdapter(Activity context, String[] date) {
    super(context, R.layout.row, date);
    this.context = context;
    formatter = new SimpleDateFormat(dateFormat);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.row, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.FilePath);
        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

    String s = formattedDateString;

    holder.text.setText(s);

        return rowView;
    }
}

截至目前,它只显示所有文件上目录的最后修改日期。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:4)

在您的问题中,您指的是目录,而不是文件。

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);

想法是获取目录并迭代搜索所有文件的上次修改日期。以下问题可能有所帮助: How to get only 10 last modified files from directory using Java?

编辑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.

编辑2:

对于您的情况,这会有所帮助:

public class MyAdapter extends ArrayAdapter<String> {

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;
        }

    }); 
}   

//现在您有一个 filelist 文件数组。如果您需要lastModified数据,可以像以前一样从每个单独的文件中获取:

  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";

答案 1 :(得分:3)

完成代码以获取上次修改日期

    File file = new File("c:\\test\\myfile.txt");
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
    System.out.println("Modified Date :- " + sdf.format(file.lastModified()));

答案 2 :(得分:0)

File file = new File(Environment.getExternalStorageDirectory() + dir);

指向目录,而不是文件。

如果您需要在目录中单独修改每个文件的日期,只需使用 walkfiletree

检查this

答案 3 :(得分:0)

我尝试了Anonymous Mohit的答案,但它只检查文件夹中项目的文件大小。但是,如果有很多子文件夹呢?这是我如何做到的。

public static long lastModifiedDate;
public static long getLastModified(String path) {
    File directory = new File(path);
    File[] files = directory.listFiles();
    if(GlobalValues.DEBUG) Log.i(TAG, "Get last modified date: " + path);
    if (files.length == 0) return directory.lastModified();
    else {
        for(File filesInFolder: files) {
            if(filesInFolder.isDirectory()) {
                getLastModified(filesInFolder.getAbsolutePath());
            } else {
                if(lastModifiedDate == 0) {
                    lastModifiedDate = filesInFolder.lastModified();
                } else{
                    if(filesInFolder.lastModified() >= lastModifiedDate){
                        lastModifiedDate = filesInFolder.lastModified();
                    }
                }
            }
        }
        return lastModifiedDate;
    }
}