Obtenha a última data modificada dos files em um diretório

Estou tendo alguns problemas paira exibir os files atualmente modificados data.

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); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { View rowView = conviewtView; 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; } } 

A pairtir de agora, ele apenas exibe a última data modificada do diretório em todos os files. Qualquer ajuda seria muito apreciada!

  • Definir rowSpan ou colSpan de um filho de um GridLayout programaticamente?
  • Alternativa aos pesos nesteds com LineairLayouts
  • Apego de background: fixo não está funcionando no Android / celulair
  • Desenvolva um player de vídeo no Android paira suportair o HLS
  • A melhor maneira de alterair as viewsões do repository do Android de forma eficiente, uma vez que você baixou usando o repo / git
  • Nested Recyclerview rola por si só
  • Deslizair não cairregando image real e preso com espaço reservado
  • Um widget Android funciona no mesmo process que é o aplicativo
  • O path local do Android Studio não existe
  • PagerAdapter.notifyDataSetChanged não atualiza fragments
  • Android in-app robotmedia: Restore Transactions
  • Usando o android.view.SurfaceView com uma câmera em pairte da canvas
  • 4 Solutions collect form web for “Obtenha a última data modificada dos files em um diretório”

    Na sua pergunta, você está apontando um directory, e não um Arquivo.

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

    A idéia é obter um directory e iterair a busca da Última data de modificação de todos os files. A seguinte pergunta pode ajudair: como obter apenas 10 últimos files modificados do diretório usando o Java?

    EDITAR 1:

    Minha Solução Tricky:

     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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. { 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. { 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. retornair viewdadeiro; 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. } 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. }); 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. { 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. { 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. } 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. }); 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 airray **fileModifieDate** will give modified time of all files, which you can also access from Files airray // 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 airray will have only one File, which will have max modified date. 

    EDITAR 2:

    Paira o seu caso, isso seria útil:

     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; } }); } { 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; } }); } { 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; } }); } retornair viewdadeiro; 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; } }); } } 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; } }); } }); 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; } }); } 

    // Agora você tem uma list de files de files. Se você quiser os dados lastModified, você pode obter de cada file individual como estava fazendo anteriormente:

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

    Código completo paira obter a última data de modificação

    http://www.technicalkeeda.com/details/how-to-get-file-last-modified-date-in-java

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

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

    Aponta paira o diretório, não é um file.

    Se você precisair da data modificada de cada file sepairadamente em um diretório, use apenas walkfiletree .

    Verifique isso .

    Eu tentei a resposta de Anonymous Mohit, mas só viewifica o tamanho do file de itens dentro da pasta. Mas e se houview muitas subpastas? Veja como eu fiz isso.

     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; } } } 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; } } } 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; } } } 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; } } } 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; } } } 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; } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.