Marcel Chasiguasin wrote:for free memory and close the connection. do I only need execute this code
bf.flush();
bf.close();
or this
pw.flush();
pw.close();
???
You only need to call close on the outermost wrapper object. So, as you have a FileWriter wrapped by a BufferedWriter wrapped by a PrintWriter, calling close on your PrintWriter will also close the FileWriter and BufferedWriter.
I think on most writers calling close will automatically do a flush, but it won't do any harm to call it explicitly.