If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.

As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see

FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

You should try creating a path for the folder it contains before:

File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();

PrintWriter printWriter = new PrintWriter(file);
Answer from Jack on Stack Overflow
🌐
Programiz
programiz.com › java-programming › printwriter
Java PrintWriter (With Examples)
In this tutorial, we will learn about Java PrintWriter and its print() and printf() methods with the help of examples to print output data.
🌐
CodeGym
codegym.cc › java blog › java io & nio › java printwriter class
Java PrintWriter Class
January 9, 2025 - It is crucial to close the PrintWriter after completing file operations to: Free System Resources: Ensures that file descriptors and other resources are released. Flush Data: Guarantees that any buffered data is written to the file. Prevent Data Corruption: Minimizes the risk of incomplete writes or corrupted files. import java.io.File; import java.io.PrintWriter; public class EnsureCloseExample { public static void main(String[] args) { PrintWriter writer = null; try { // Create and initialize PrintWriter File file = new File("safeclose.txt"); writer = new PrintWriter(file); // Write data to the file writer.println("Always close resources properly."); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } finally { // Ensure the PrintWriter is closed if (writer != null) { writer.close(); } } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-printwriter-class-java-set-1
Java.io.PrintWriter class in Java | Set 1 - GeeksforGeeks
July 23, 2025 - PrintWriter(Writer out, boolean autoFlush): Creates a new PrintWriter. Methods of PrinterWriter Class in Java are mentioned below:
🌐
Codecademy
codecademy.com › docs › java › printwriter
Java | PrintWriter | Codecademy
August 17, 2024 - Learn to code in Java — a robust ... Friendly.Beginner Friendly17 hours17 hours · PrintWriter writer = new PrintWriter(new File(file_name)); writer.println(text); writer.close();...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › PrintWriter.html
PrintWriter (Java Platform SE 8 )
1 month ago - Creates a new PrintWriter, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
🌐
Edureka
edureka.co › blog › printwriter-class-in-java
PrintWriter Class in Java | Java.io.PrintWriter Class Example | Edureka
October 7, 2019 - Java Tutorial For Beginners – Java Programming Made Easy! ... The implementation of the writer class in Java is the PrintWriter Class. The formatted representation of objects is printed to a text output stream. Let us dig a bit deeper and understand the concept in detail.
🌐
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.
Find elsewhere
🌐
ZetCode
zetcode.com › java › io-printwriter
Java PrintWriter Class - Complete Tutorial with Examples
Complete Java PrintWriter class tutorial covering all methods with examples. Learn about formatted output operations in Java I/O.
🌐
Jenkov
jenkov.com › tutorials › java-io › printwriter.html
Java IO: PrintWriter
September 10, 2015 - The Java PrintWriter class (java.io.PrintWriter) enables you to write formatted data to an underlying Writer.
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 22 & JDK 22)
July 16, 2024 - 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.
🌐
Codemistic
codemistic.github.io › java › print-writer-java.html
Java PrintWriter | Java Tutorials | CodeMistic
In this tutorial, we will learn about the Java PrintWriter, its constructors and its methods with the help of an example.
🌐
Tutorialspoint
tutorialspoint.com › home › java/io › java printwriter class
Java.io.PrintWriter Class
February 13, 2026 - Learn about the Java PrintWriter class, its methods, and how to use it for output formatting in Java applications.
🌐
DZone
dzone.com › coding › java › java printwriter with example
Java PrintWriter With Example
February 19, 2020 - In this article, we discuss Java's PrintWriter class, its methods, and its differences compared to PrintStream.
🌐
Scientech Easy
scientecheasy.com › home › blog › printwriter in java (with example)
PrintWriter in Java (with Example) - Scientech Easy
February 10, 2025 - Learn PrintWriter in Java with example program, Java PrintWriter class declaration, constructors, methods defined by PrintWriter class
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java printwriter
Java PrintWriter | Constructor, Methods and Examples of Java PrintWriter
April 10, 2023 - Guide to Java PrintWriter. Here we discuss the introduction to java PrintWriter, constructors, methods and examples for better understanding.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
CodeAhoy
codeahoy.com › java › PrintWriter-JI_10
Java PrintWriter Explained with Examples (Java 9+) | CodeAhoy
October 26, 2016 - java.io.PrintWriter class explained with examples of how to use it to write formatted data to files.
🌐
Baeldung
baeldung.com › home › java › java io › printwriter write() vs print() method in java
PrintWriter write() vs print() Method in Java | Baeldung
April 16, 2024 - In this tutorial, we’ve explored PrintWriter class and, more specifically, the write() and print() methods. As we saw, the PrintWriter class makes available several methods that help us print data to the output.
🌐
Scaler
scaler.com › home › topics › java class printwriter
Java Class PrintWriter - Scaler Topics
December 19, 2022 - In the above code, we have used different PrintWriter methods to print different data types to the console. We can write formatted data to an underlying Writer using the Java PrintWriter class java.io.PrintWriter.
🌐
GeeksforGeeks
geeksforgeeks.org › java › printwriter-printlnstring-method-in-java-with-examples
PrintWriter println(String) method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // PrintWriter println(String) method import java.io.*; class GFG { public static void main(String[] args) { try { // Create a PrintWriter instance PrintWriter writer = new PrintWriter(System.out); // Get the String ...