Why is Files.lines (and similar Streams) not automatically closed?

前端 未结 4 1455
再見小時候
再見小時候 2020-11-27 16:23

The javadoc for Stream states:

Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually n

相关标签:
4条回答
  • 2020-11-27 16:53

    I have more specific example in addition to @BrianGoetz answer. Don't forget that the Stream has escape-hatch methods like iterator(). Suppose you are doing this:

    Iterator<String> iterator = Files.lines(path).iterator();
    

    After that you may call hasNext() and next() several times, then just abandon this iterator: Iterator interface perfectly supports such use. There's no way to explicitly close the Iterator, the only object you can close here is the Stream. So this way it would work perfectly fine:

    try(Stream<String> stream = Files.lines(path)) {
        Iterator<String> iterator = stream.iterator();
        // use iterator in any way you want and abandon it at any moment
    } // file is correctly closed here.
    
    0 讨论(0)
  • 2020-11-27 17:03

    Yes, this was a deliberate decision. We considered both alternatives.

    The operating design principle here is "whoever acquires the resource should release the resource". Files don't auto-close when you read to EOF; we expect files to be closed explicitly by whoever opened them. Streams that are backed by IO resources are the same.

    Fortunately, the language provides a mechanism for automating this for you: try-with-resources. Because Stream implements AutoCloseable, you can do:

    try (Stream<String> s = Files.lines(...)) {
        s.forEach(...);
    }
    

    The argument that "it would be really convenient to auto-close so I could write it as a one-liner" is nice, but would mostly be the tail wagging the dog. If you opened a file or other resource, you should also be prepared to close it. Effective and consistent resource management trumps "I want to write this in one line", and we chose not to distort the design just to preserve the one-line-ness.

    0 讨论(0)
  • 2020-11-27 17:05

    If you're lazy like me and don't mind the "if an exception is raised, it will leave the file handle open" you could wrap the stream in an autoclosing stream, something like this (there may be other ways):

      static Stream<String> allLinesCloseAtEnd(String filename) throws IOException {
        Stream<String> lines = Files.lines(Paths.get(filename));
        Iterator<String> linesIter = lines.iterator();
    
        Iterator it = new Iterator() {
          @Override
          public boolean hasNext() {
            if (!linesIter.hasNext()) {
              lines.close(); // auto-close when reach end
              return false;
            }
            return true;
          }
    
          @Override
          public Object next() {
            return linesIter.next();
          }
        };
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT), false);
      }
    
    0 讨论(0)
  • 2020-11-27 17:15

    In addition if you want "one line write". You can just do this:

    Files.readAllLines(source).stream().forEach(...);
    

    You can use it if you are sure that you need entire file and the file is small. Because it isn't a lazy read.

    0 讨论(0)
提交回复
热议问题