调用close()会导致刷新所有数据.你构建了一个PrintWriter而没有启用自动刷新(其中一个构造函数的第二个参数),这意味着你必须手动调用flush(),close()会为你做.
关闭还可以释放打开文件所使用的任何系统资源.虽然VM和操作系统最终将关闭该文件,但最好在完成后关闭它以节省计算机上的内存.
你也可以把close()放在finally块中,以确保它总是被调用.如:
PrintWriter out = null;
try {
File f = new File("c:/users/dell/desktop/ja/MyLOgs.txt");
out = new PrintWriter(new FileWriter(f,true));
out.println("the name of the user is "+name+"\n");
out.println("the email of the user is "+ email+"\n");
} finally {
out.close();
}
Sanchit也提出了一个很好的观点,让Java 7 VM能够在您不需要它们的情况下自动关闭流.