So I have this fairly simple code where I'm trying to read a file line by line:
if(new File(filesDir).listFiles() != null){
for(File file : new File(filesDir).listFiles()){
try {
Stream<String> stream = Files.lines(Paths.get(file.getAbsolutePath()));
for(int i = 0 ; i < stream.toArray().length; i++){
System.out.println(stream.toArray()[i]);
}
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
But when it reaches Sysout
it throws exception:
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: stream has already been operated upon or closed
Caused by: java.lang.IllegalStateException: stream has already been operated upon or closed
Every example I find uses Lambda expressions, I don't think that's the only way, or is it?