FilternameFilter in Java

0

I am doing a program that looks for the names of files in a directory and I need to pass as a parameter a String that indicates part of the name of the file, but that does not necessarily start with that parameter. For example, I pass the parameter "123456", and it returns the similar ones, like abc123456, 123456abc, etc. I would appreciate if someone can help me.

    
asked by Carlos 13.02.2017 в 14:44
source

2 answers

2

You can use File#listFiles(FilenameFilter) to obtain the files and apply a filter for the names to obtain:

public File[] obtenerArchivosFiltrados(File carpetaBase, String nombre) {
    return carpetaBase.listFiles( (f, s) -> s.contains(nombre) );
}
    
answered by 13.02.2017 в 15:33
1

You can do this if you use Java 8:

    public static List<String> buscaCoincidencia(List<String> listaArchivos,String palabraClave)
    {
       listaArchivos.removeIf((s -> !s.contains(palabraClave)));
       return listaArchivos;
    }
    
answered by 13.02.2017 в 14:59