0

I'm currently working on an assignment which consists of creating a utility class with a method allowing to search for files/directories by name in a given (as a parameter) directory.

The drill is that I am obligated to do this within the realms of functional programming / stream processing.

I have tried to achieve this using .walk() and .find() but it wouldn't work

public static List<File> findFile(Path path, String name) throws IOException{

    return Files.walk(path)
            .filter(n -> n.getFileName().toString().equals(name))
            .map(n -> n.toFile())
            .collect(Collectors.toList());
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57

3 Answers3

2

Check out this question

File dir= new File("path");
File[] fileList = dir.listFiles(new FilenameFilter() 
{
    public boolean accept(File dir, String foundFileName) 
   {
        return name.equalsIgnoreCase(foundFileName);
   }
});

Although it seems you're just trying to search for one specific file, so it isn't the best code for this task. Anyway, your file should be in the first position of the array. Or make some nasty dir.listFiles()..[0]

aran
  • 10,978
  • 5
  • 39
  • 69
0

You can instead use Files.list(Path dir)

List<File> listOfFiles = Files.list(Paths.get(path))
                              .filter(e -> e.getFileName().endsWith(name))
                              .map(n -> n.toFile()).collect(Collectors.toList());

Note:

Tweak the filter condition as per your requirement. I have assumed name to have a value like .txt, so the listOfFiles will contain all files that end with .txt

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • Thank you @Nicholas! That is way cleaner version of what i have figured out: return Arrays.stream(path.toFile().listFiles()).filter(n -> n.getName().equals(name)).collect(Collectors.toList()); I forgot to mention though, that I want to make it recursive - do you have any idea how do I achieve this? – hobbledehoy Jan 15 '19 at 10:22
  • Recursive? Can you give me an example of what you are trying to achieve? – Nicholas K Jan 15 '19 at 10:24
  • What I mean is that if I understand FIles.list() correctly it won't find files of the given name that are 'deeper' in the file tree. I want to make it return not only file 'name' inside the path folder, but also inside path/foo/ and path/foo/bar etc. – hobbledehoy Jan 15 '19 at 10:31
  • 1
    @NicholasK now, it’s the OP’s turn to explain which actual problem they encountered. – Holger Jan 15 '19 at 10:41
  • @hobbledehoy : [This](https://stackoverflow.com/a/33469854/6139527) answers that part of your question. – Nicholas K Jan 15 '19 at 11:08
0

The walk method will work for you, but converting to file at the beginning lets you to use the getName method which returns the File name as a String

public static List<File> findFile(Path path, String name) throws IOException{
    return Files
        .walk(path)
        .map(Path::toFile)    //.map(p -> p.toFile())
        .filter(File::isFile) //.filter(f -> f.isFile())
        .filter(f -> f.getName().equals(name))
        .collect(Collectors.toList());
}

Note that the provided name must be exactly the same of the file (including its extension) or the function won't find it

DSantiagoBC
  • 464
  • 2
  • 11