I have a ArrayList<String>
which I love files with extensions .pnd .ana .cnf with their respective names. What I need to know is how to ask if there are three extensions within the ArrayList, only the extensions and NO the full file name with its respective extension, for example: find the .pnd and .ana extensions that do nothing, if you find .pnd and .cnf do not do anything but if within the array there are only .pnd extension files, you just have to perform an action , I mean? So far and I do the following:
private void evaluarArchivos() {
String pnd = ".pnd";
String ana = ".ana";
String cnf = ".cnf";
private boolean archivoExistente = true;
for (String evaluar : arrayArchivos) {
if (evaluar.contains(pnd) || evaluar.contains(ana)) {
archivoExistente = false;
System.out.println("Nada");
}
if (evaluar.contains(pnd) || evaluar.contains(cnf)) {
archivoExistente = false;
System.out.println("Nada 2");
}
if (evaluar.contains(pnd)) {
archivoExistente = true;
System.out.println("Hacer algo");
}
}
}
I use the contains
property but it always returns true
since the three extensions are always within the arraylist, and for every turn that my search method gives, it is added what I had plus what is added again.
public void buscarArchivo(File ruta) {
// Creo el vector que contendra todos los archivos de una ruta especificada.
File[] archivo = ruta.listFiles();
// Evaluo si la carpeta especificada contiene archivos.
if (archivo != null) {
// Recorro el vector el cual tiene almacenado la ruta del archivo a buscar.
for (int i = 0; i < archivo.length; i++) {
File Arc = archivo[i];
// Evaluo si el archivo o la ruta es una carpeta.
if (archivo[i].isDirectory()) {
// Le paso la nueva ruta de la carpeta si se cambia la ruta e busca nuevamente.
buscarArchivo(archivo[i]);
} else {
// Evaluo el tipo de extencion.
if (archivo[i].getName().endsWith(".pnd") || archivo[i].getName().endsWith(".ana") || archivo[i].getName().endsWith(".cnf")) {
contador++;
arrayArchivos.add(archivo[i].getName());
// System.out.println("Lo que hay en el array es: " + arrayArchivos);
evaluarArchivos();
}
}
}
}
}
I think maybe it gives me true
because in every turn of my bugle my method searchFile constantly adds new files leaving the old ones that searched for in the previous round, how could I avoid that? It would be that for each lap of the bugle I added to the array, I evaluated it and eliminated it, on the next lap I did the same and so on. Only have in the array the files that are in each round that it gives and not the previous ones . I do not know if I make myself understand ...
Another solution that I thought was to do an if inside the bugle evaluating if not all the extensions end with .pnd by setting my variable archivoExistente = false
if (archivo[i].getName().endsWith(".pnd") || archivo[i].getName().endsWith(".ana") || archivo[i].getName().endsWith(".cnf")) {
if(!archivo[i].getName().endsWith(".pnd")){
// Si no termina con .pnd implica que no todos los archivos son de esa extensión
archivoExistente = false;
System.out.println("Nada");
}
contador++;
arrayArchivos.add(archivo[i].getName());
}
But it does not work for me because it only adds extensions of type .pnd and what I need is to evaluate the type of files inside the folder before adding it or adding all the files that are inside the folder and then evaluate what type of extension they are . I hope you can help me. Thanks