public List<T> collect() { List<T> list = new ArrayList<>(); for (Pair<String, String> p : map.keySet()) { String folder = p.first; String pattern = p.second; Function<String, ? extends T> func = map.get(p); DirectoryStream<Path> dirStream; try { dirStream = Files.newDirectoryStream(Paths.get(folder), pattern); logger.info(String.format("Load files from folder %s with pattern %s.", folder, pattern)); // sort the files List<String> files = new ArrayList<>(); dirStream.forEach(s -> files.add(s.toString())); Collections.sort(files); for (String s : files) { logger.info(String.format("Loading file %s.", s)); list.add(func.apply(s)); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(String.format("Could not load files from patern: %s", pattern)); } } return list; }
private static List<Path> filesInDirectory(String resource) throws IOException, URISyntaxException { List<Path> files = new ArrayList<>(); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream( Paths.get(RandomKnapsackTest.class.getResource(resource).toURI()))) { directoryStream.forEach(files::add); } return files; }