Scanner is never closed

I am assuming you are using java 7, thus you get a compiler warning, when you don’t close the resource you should close your scanner usually in a finally block. Scanner scanner = null; try { scanner = new Scanner(System.in); //rest of the code } finally { if(scanner!=null) scanner.close(); } Or even better: use the … Read more

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

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 … Read more