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 ...
🌐
Tutorialspoint
tutorialspoint.com › java › io › printwriter_close.htm
Java.io.PrintWriter.close() Method
package com.tutorialspoint; import java.io.*; public class PrintWriterDemo { public static void main(String[] args) { String s = "Hello World "; try { // create a new stream at system PrintWriter pw = new PrintWriter(System.out); // append the sequence pw.append(s); // flush the writer pw.flush(); // print another string pw.println("This is an example"); // flush the writer again pw.flush(); // close the writer pw.close(); } catch (Exception ex) { ex.printStackTrace(); } } } Let us compile and run the above program, this will produce the following result − ·
🌐
Tabnine
tabnine.com › home page › code › java › java.io.printwriter
java.io.PrintWriter.close java code examples | Tabnine
public static void itemsToFile(OutputStream os, List<String> items) { try { PrintWriter printWriter = new PrintWriter(os); items.forEach(printWriter::println); printWriter.close(); } catch (Exception e) { throw e; } }
🌐
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 ...
🌐
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)
Calling flush() tells PrintWriter to write the buffered data to the file (actually it tells FileWriter to write the data to the file). Calling close() tells PrintWriter to clean up its resources. Close() also does a flush, so you don't necessarily need to call flush(). You should always call ...
🌐
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.
🌐
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
Find elsewhere
🌐
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 ·
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › PrintWriter.html
PrintWriter (Java Platform SE 7 )
Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
🌐
Jenkov
jenkov.com › tutorials › java-io › printwriter.html
Java IO: PrintWriter
September 10, 2015 - Closing a PrintWriter is done by calling its close() method. Here is how closing a PrintWriter looks: ... You can also use the try-with-resources construct introduced in Java 7.
🌐
Stack Overflow
stackoverflow.com › questions › 31942535 › closing-a-printwriter-object-that-has-been-initialized-in-an-if-statement
java - Closing a PrintWriter Object that has been initialized in an if statement - Stack Overflow
But that raises the question, why are you recreating the PrintWriter each time through the loop? Just create it once, outside of the loop. The other problem is that none of your cleanup code is actually executed if an exception is thrown. What you need is a try-catch-finally block, where you can guarantee that your cleanup code is executed. If you're using at least Java 7, you can use try-with-resources, which will take care of all the cleanup for you automatically:
🌐
Tutorialspoint
tutorialspoint.com › home › java/io › java writer close method
Java - Writer close() method
February 13, 2026 - The following example shows the usage of Writer close() method. package com.tutorialspoint; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; public class WriterDemo { public static void main(String[] args) { String ...
🌐
Stack Overflow
stackoverflow.com › questions › 13571301 › printwriter-eclipse
java - PrintWriter eclipse - Stack Overflow
I'm using Eclipse to write code that needs to write to an output file. the file name comes from a command-line argument. I don't know where it is creating the file. It says that my workspace is the default for the output file but it is no where to be found. This is my code · try{ PrintWriter output = new PrintWriter(new File(args[3])); //System.out.print(args[3]); output.append('l'); output.close(); } catch(IOException e) { System.exit(0); } java ·
🌐
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 - PrintWriter in Java is a character-output class that writes formatted text to an underlying destination like a file, socket, or console. The key question many developers ask is: “When I use PrintWriter, what exactly do I need to close – the PrintWriter, the underlying stream, or both?” In modern Java (up to Java 21 and current best 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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.printwriter.close
PrintWriter.Close Method (Java.IO) | Microsoft Learn
Closes the stream and releases any system resources associated with it. Closing a previously closed stream has no effect. Java documentation for java.io.PrintWriter.close().