12

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

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

4 回答 4

8

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

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

这个想法是获取一个目录并迭代搜索所有文件的最后修改日期。以下问题可能会有所帮助: 如何使用 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:

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

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

    }); 
}   

// 现在你有了一个文件列表数组。如果您想要 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";
于 2013-09-14T17:01:19.880 回答
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()));
于 2014-03-15T07:56:00.313 回答
0

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

指向目录,而不是文件。

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

检查这个

于 2013-09-14T16:47:39.023 回答
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;
    }
}
于 2015-07-03T06:49:02.003 回答