Need to read several files. Full paths to them lie in a certain ArrayList.

try (Scanner scanner = new Scanner(new File(//сюда нужно воткнуть элементы Arraylist))) { 

What needs to be done in order for the program to accept ArrayList elements as parameters?

  • one
    In the cycle transmit - rjhdby

3 answers 3

 for (File file : list) { try (Scanner scanner = new Scanner(file)) { ... } } 

    File class corresponds to one file. It is necessary to bypass ArrayList elements in a loop and for each one to perform the necessary operations.

      Option with stream api , for reading the contents of files in the files list

       files.stream() .flatMap(file -> { try { return Files.lines(file.toPath()); } catch (IOException e) { e.printStackTrace(); return Stream.of(""); }}) .forEach(System.out::println);