nio - Java 8 Path Stream and FileSystemException (Too many open files) -


geniuses!

i'm practicing java 8.

so if this:

files.walk(paths.get(corpuspathstr))         .filter(path -> path.tofile().isfile())         .foreach(path -> {             try {                 files.lines(path)                         .foreach(...);             } catch (ioexception e) {                 e.printstacktrace();             }         }); 

i got filesystemexception error.

if open file under foreach, may many files opened?

or there other reasons causing filesystemexception (too many open files)?

thanks in advance!

use

try(stream<path> stream = files.walk(paths.get(corpuspathstr))) {     stream.filter(path -> files.isregularfile(path) && files.isreadable(path))           .flatmap(path -> {                        try { return files.lines(path); }                        catch (ioexception e) { throw new uncheckedioexception(e); }                    })           .foreach(...); } catch(uncheckedioexception ex) {     throw ex.getcause(); } 

the streams returned files.walk , files.lines must closed release resources, either, try(…) construct or returning them in mapping function of flatmap operation.

don’t use nested foreachs.

the uncheckedioexception might not thrown our mapping function, stream implementations. translating ioexception allows treat them equally.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -