简体   繁体   中英

Like Python, is there any way in Java to read/write without closing buffer?

1) Like Python, is there any way in Java to read/write without closing the buffer? As http://blog.lerner.co.il/dont-use-python-close-files-answer-depends article explains, by deleting the object, or manipulating the data size in comparison to the buffer size, we can still get the job done without closing the buffer. What's the general intuition/theory behind this concept? How is it possible? Why exactly should we close the buffer?
2) Could I rephrase/add to my question? Please enlighten.

This is equivalent to your python code. file is automatically closed.

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
    stream.forEach(System.out::println);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM