We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I need to filter an ArrayList of Files by png images, and I need to order it from oldest to most recent, but it would be nice if it ordered not by last modified but by creation date.
This is the relevant code (variable names and methods in Spanish, sorry):
AdminImagen class
import java.io.File;
import java.util.Date;
import java.util.ArrayList;
class AdminImagen {
ArrayList<File> archivos;
ArrayList<Imagen> imagenes;
AdminImagen(String ruta) {
archivos=listarArchivos(ruta);
imagenes=prepararImagenes(archivos);
}
ArrayList<File>listarArchivos(String ruta) {
File file=new File(ruta);
ArrayList<File> lista = new ArrayList<File>();
if (file.isDirectory()) {
File[] arch=file.listFiles();
for (int i=0;i<arch.length;i++) {
lista.add(arch[i]);
}
return lista;
}
else return null;
}
ArrayList<Imagen> prepararImagenes(ArrayList<File> arch) {
ArrayList<Imagen> im=new ArrayList<Imagen>();
for (File f:arch) {
String ruta=f.getAbsolutePath();
Date fecha=new Date(f.lastModified());
Imagen imagen=new Imagen(ruta, fecha);
im.add(imagen);
}
return im;
}
String getRutaImagen(int a) {
return imagenes.get(a).rutaArchivo;
}
Date getFechaImagen(int a) {
return imagenes.get(a).hora;
}
String getHoraImagen(int a) {
Date fecha = getFechaImagen(a);
String hora = fecha.getHours()+":"+fecha.getMinutes();
return hora;
}
}
Imagen class
import java.io.File;
import java.util.Date;
class Imagen {
String rutaArchivo;
Date hora;
Imagen(String ruta, Date fecha) {
rutaArchivo=ruta;
hora=fecha;
}
Imagen() {
rutaArchivo="";
hora=null;
}
//crear un objeto Imagen a partir de un File
void extraerInfoImagen(File archivo) {
rutaArchivo=archivo.getAbsolutePath();
hora=new Date(archivo.lastModified());
}
}
I really don't know how to make these operations, so could you help me out here? Thanks
Answers
As you mighta seen from http://forum.processing.org/one/topic/listing-last-10-modified-files-in-directory,
we gotta use a FilenameFilter reference as an argument for listFiles() method! :-\"
Here are some relevant parts from my posted example there:
Now for the sorting part, we're gonna need both File[] + Comparator references as arguments for Arrays.sort() method. =:)
There, I've chosen to use a Map<File, Long> to temporarily store each File w/ its corresponding lastModified() value. :ar!
Again, some relevant snippets from my full code:
Your code was spot on, had to remove the final static parts so that it would work in my particular case, but the rest was used as is. Thanks a lot, I'll credit you in my proyect :)
sorry for double post.