You can use a try-with-resources block

try (PrintWriter fileOut = new PrintWriter(new BufferedWriter(new     FileWriter(csvFileIn)))) {
    //WHATEVER you need to do
}

Since PrintWriter implements AutoCloseable it will close by itself once the try block is complete (even if an exception is raised)

Check more info about this here

Answer from SCouto on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › printwriter-close-method-in-java-with-examples
PrintWriter close() method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // PrintWriter close() method import java.io.*; class GFG { public static void main(String[] args) { try { // Create a PrintWriter instance PrintWriter writer = new PrintWriter(System.out); // Write the char to this writer // This will put the char in the stream // till it is printed on the console writer.write(65); // Now close the stream // using close() method writer.close(); } catch (Exception e) { System.out.println(e); } } }
🌐
Tutorialspoint
tutorialspoint.com › java › io › printwriter_close.htm
Java.io.PrintWriter.close() Method
The java.io.PrintWriter.close() method closes the stream and releases any system resources associated with it.
🌐
Coderanch
coderanch.com › t › 499845 › certification › PrintWriter-flush-close-code-work
PrintWriter using flush() and close() for code to work [Solved] (OCPJP forum at Coderanch)
Close() also does a flush, so you don't necessarily need to call flush(). You should always call close(). And you should call close() in a finalize block to ensure you always call it (even if an exception is thrown). BTW, you can also use a different PrintWriter constructor that will tell PrintWriter to flush every time you call pw.println().
🌐
Tabnine
tabnine.com › home page › code › java › java.io.printwriter
java.io.PrintWriter.close java code examples | Tabnine
Best Java code snippets using java.io.PrintWriter.close (Showing top 20 results out of 26,874) · PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); w.print(line); w.flush(); w.close(); · Closes this print writer.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › PrintWriter.html
PrintWriter (Java Platform SE 8 )
1 month ago - java.io.PrintWriter · All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable · public class PrintWriter extends Writer · Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.
🌐
Educative
educative.io › answers › what-is-the-writerclose-method-in-java
What is the writer.close() method in Java?
Writer writer = new PrintWriter(System.out); ... // call the close() method · writer.close() In the code above, we create a writer instance and call the close() method on it. This method does not accept any parameters.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › PrintWriter.html
PrintWriter (Java Platform SE 7 )
java.io.PrintWriter · All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable · public class PrintWriter extends Writer · Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.
🌐
IncludeHelp
includehelp.com › java › printwriter-close-method-with-example.aspx
Java PrintWriter close() Method with Example
April 19, 2020 - // Java program to demonstrate the example // of void close() method of PrintWriter import java.io.*; public class CloseOfPW { public static void main(String[] args) { String str = "Java Programming"; // Instantiates PrintWriter PrintWriter p_stm = new PrintWriter(System.out); // Display str p_stm.println("str: " + str); p_stm.flush(); // By using close() method is to // close the stream p_stm System.out.println("Stream Shutdown...."); p_stm.close(); } } Output ·
Find elsewhere
🌐
Jenkov
jenkov.com › tutorials › java-io › printwriter.html
Java IO: PrintWriter
September 10, 2015 - That means that the try-with-resources block will not automatically close this FileWriter instance. However, when the PrintWriter is closed it will also close the OutputStream instance it writes to, so the FileWriter instance will get closed when the PrintWriter is closed.
🌐
Javatpoint
javatpoint.com › java-printwriter-close-method
Java PrintWriter close() Method with Examples - Javatpoint
Java PrintWriter append() checkError() close() flush() print() println() write() For Videos Join Our Youtube Channel: Join Now · Send your Feedback to [email protected] Splunk · SPSS · Swagger · Transact-SQL · Tumblr · ReactJS · Regex · Reinforcement Learning ·
🌐
Tutorialspoint
tutorialspoint.com › home › java/io › java writer close method
Java - Writer close() method
September 1, 2008 - The Java Writer close() method closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown.
🌐
Coderanch
coderanch.com › t › 382563 › java › closing-PrintWriter-cuts-output
closing PrintWriter cuts output (Java in General forum at Coderanch)
April 29, 2007 - You can't. Closing a PrintWriter (or any PrintStream, FilterWriter, FilterReader, or FilterXXXStream) has the effect of closing the underlying stream. The docs aren't 100% clear here (they just say it "releases any resources") but that's what happens. I would say that your StreamEater really ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-printwriter-class-java-set-1
Java.io.PrintWriter class in Java | Set 1 - GeeksforGeeks
July 23, 2025 - Syntax :public void close() Specified by:close in class Writer · Flushes the stream. Syntax :public void flush() Specified by:flush in interface Flushable Specified by:flush in class Writer · Writes a formatted string to this writer using ...
🌐
Coderanch
coderanch.com › t › 349132 › java › close-PrintWriter-servlet
Must close PrintWriter in servlet when done? (Servlets forum at Coderanch)
February 20, 2001 - There is no need for explixitly closing the PrintWriter object as once all the response has been send,the Http connection closes and the stram gets closed.But it's a good coding practice to close all the resources you have opened in the program before you exit bye sam
🌐
CopyProgramming
copyprogramming.com › howto › what-do-i-need-to-close-when-using-printwriter-in-java-duplicate
Java `PrintWriter.close`: What You Really Need to Close in 2026 - Java close printwriter
January 2, 2026 - The key question many developers ... practices in 2026), the definitive answer is that you close the outermost resource (usually the PrintWriter) and let it close the underlying streams, ideally via try‑with‑resources....
Top answer
1 of 6
4

Calling close() causes all the data to be flushed. You have constructed a PrintWriter without enabling auto-flush (a second argument to one of the constructors), which would mean you would have to manually call flush(), which close() does for you.

Closing also frees up any system resources used by having the file open. Although the VM and Operating System will eventually close the file, it is good practice to close it when you are finished with it to save memory on the computer.

You may also which to put the close() inside a finally block to ensure it always gets called. Such as:

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();
}

See: PrintWriter

Sanchit also makes a good point about getting the Java 7 VM to automatically close your streams the moment you don't need them automatically.

2 of 6
3

When you close a PrintWriter, it will flush all of its data out to wherever you want the data to go. It doesn't automatically do this because if it did every time you wrote to something, it would be very inefficient as writing is not an easy process.

You could achieve the same effect with flush();, but you should always close streams - see here: http://www.javapractices.com/topic/TopicAction.do?Id=8 and here: http://docs.oracle.com/javase/tutorial/jndi/ldap/close.html. Always call close(); on streams when you are done using them. Additionally, to make sure it is always closed regardless of exceptions, you could do this:

try {
    //do stuff
} finally {
    outputStream.close():
}