0

I have this code below that is throwing stream has already been operated upon or closed

    private static void readDataInputFile(String fileName) throws IOException {
    List<String> rowList = new ArrayList<>();

    try (Stream<String> inputStream = Files.lines(Paths.get(fileName))) {
        Optional<String> headerOptional = inputStream.findFirst();
        String header = headerOptional.get();
        int headerColumnCount = header.split(Constants.SEPERATOR_ESCAPED).length;
        rowList = inputStream.collect(Collectors.toList());
        for (String string : rowList) {
            System.out.println(string);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I know we can't reuse the stream. I used the stream at inputStream.findFirst(); as well as at inputStream.collect(Collectors.toList()).

What do I do in this case? Should I create another stream or is there a way to copy the stream?

Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
  • Why don't you just check for the first line in the collected list? – Sotirios Delimanolis May 16 '16 at 18:05
  • @SotiriosDelimanolis : I can do `rowList.get(0)` . however I'd like to understand the solution to this exception – Damien-Amen May 16 '16 at 18:09
  • 1
    This question is now closed, here's my answer: Since you need to read all the rows, I would recommend getting the lines in a `List` and modifying it instead of going with `Stream`. https://gist.github.com/GuiSim/a7170a69440383721a1d3b34c9922b2e – GuiSim May 16 '16 at 18:15

0 Answers0