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?