获取文件的最后修改日期在一个目录

获取文件的最后修改日期在一个目录

本文介绍了获取文件的最后修改日期在一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,显示当前文件的修改日期。

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

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

截至目前它只是显示上的所有文件目录的最后修改日期。任何帮助将大大AP preciated!

As of now it just displays the last modified date of the directory on all files. Any help would be greatly appreciated!

推荐答案

在你的问题,你都指向一个目录,而不是文件。

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

的想法是让一个目录遍历并搜索所有文件的最后修改日期。下面的问题可能会有所帮助:
<一href=\"http://stackoverflow.com/questions/5005965/how-to-get-only-10-last-modified-files-from-directory-using-java?rq=1\">How使用Java从目录中只得到10最后修改的文件?

编辑1:

我的整蛊解决方案:

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:

有关你的情况,这将是有益的:

For your case, this would be helpful:

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

    });
}

//现在你有一个文件清单:文件的数组。如果你想上次更改数据,可以为你做pviously $ P $从各个文件获取:

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

这篇关于获取文件的最后修改日期在一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:37