Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File close on JVM exit

807569Jun 11 2006 — edited Jun 12 2006
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2006
Added on Jun 11 2006
10 comments
776 views