我在下面的代码中抛出了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();
}
}
我知道我们无法重复使用该流。我在inputStream.findFirst();
以及inputStream.collect(Collectors.toList())
处使用了流。
在这种情况下我该怎么办?我应该创建另一个流还是有办法复制流?