See the following code:
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("test.txt"));
pw.write("hi");
}
}
This code does not close pw, hence no data is written to test.txt. To me,
this does not make sense, because I believe that JVM surely knows what
files are opened. Thus it wouldn't be difficult for JVM to close all files
that are opened but not closed until user application exit.
But, JVM does not close pw, and result in loss of data. This behavior
would make sense if I were writing apps w/ C or C++ because those
languages give much freedom to programmers such that people can do
whatever they want. Java, on the other hand, has the philosophy of
easy of use and safety, I believe.
Does anybody know why JVM does not try to close pw?
Sincerely,
Minkoo Seo