Search code examples
javaspringspring-bootjava-streamcomparator

Convert Resource array to File ArrayList


I am trying to read files from resource directory on a spring application.

private File[] sortResources(Resource[] resources) {
    assert resources != null;

    File[] files = Arrays.stream(resources).sorted()
            .filter(Resource::exists).filter(Resource::isFile).map(res -> {
                try {
                    return res.getFile();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
    ).toArray(File[]::new);

    for (File f : files) {
        System.out.println( f.getAbsolutePath() );
    }
    return files;
}

Using as follows:

// Read all directories inside dbdata/mongo/ directory.
Resource[] resources = resourcePatternResolver.getResources("classpath:dbdata/mongo/*");
List<File> files = sortResources(Resource[] resources);

The problem is on sortResources function. I want to sort and convert the Resources objects to Files objects.

I can't get .toArray() to work, since I get the following error:

Method threw 'java.lang.ClassCastException' exception.

class org.springframework.core.io.FileSystemResource cannot be cast to class java.lang.Comparable (org.springframework.core.io.FileSystemResource is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

I also tried .collect(Collectors.toList()) but I get the same error.

Can someone help me on that?


Solution

  • org.springframework.core.io.FileSystemResource obviously does not implement the Comparable<T> interface, so your call to sorted throws an Exception.

    You could either pass a Comparator to sorted, but it would be easier to simply move the call to sorted after the map operation, since java.io.File does implement Comparable.