When reading beginner books on Java, I have read that a stream must always be closed once it is no longer required. Why is this the case? What's wrong with leaving it open?
Consider the example below:
import java.io.*;
public class SOStreamTest {
public static void main(String[] args){
try{
FileWriter writer = new FileWriter("Foo.txt");
writer.write("hello foo!");
//Writer.close(); <-!Line in Question -->
}catch(IOException ex){
ex.printStackTrace();
}
}
}
As expected a warning message arises, 'resource leak: "writer" is never closed.'