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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-printwriter-class-java-set-1
Java.io.PrintWriter class in Java | Set 1 - GeeksforGeeks
July 23, 2025 - Java PrintWriter class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use ...
🌐
Programiz
programiz.com › java-programming › printwriter
Java PrintWriter (With Examples)
Become a certified Java programmer. Try Programiz PRO! ... The PrintWriter class of the java.io package can be used to write output data in a commonly readable form (text).
🌐
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 and charset.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › io › PrintWriter.java
jdk/src/java.base/share/classes/java/io/PrintWriter.java at master · openjdk/jdk
public PrintWriter(OutputStream out, boolean autoFlush, Charset charset) { this(new BufferedWriter(new OutputStreamWriter(out, charset)), autoFlush); · // save print stream for error propagation · if (out instanceof java.io.PrintStream) { psOut = (PrintStream) out; } } ·
Author   openjdk
🌐
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 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.
🌐
Codecademy
codecademy.com › docs › java › printwriter
Java | PrintWriter | Codecademy
August 17, 2024 - PrintWriter is a class in Java that is used for writing text in readable form.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java io & nio › java printwriter class
Java PrintWriter Class
January 9, 2025 - “PrintWriter is a class used to write any form of data e.g. int, float, double, String or Object in the form of text either on the console or in a file in Java.” For example, you may use the PrintWriter object to log data in a file or print ...
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 26 & JDK 26)
1 month ago - Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 11 & JDK 11 )
January 20, 2026 - 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.
🌐
Baeldung
baeldung.com › home › java › java io › printwriter vs. filewriter in java
PrintWriter vs. FileWriter in Java | Baeldung
December 1, 2023 - The PrintWriter class helps write formatted text to an output stream like file and console.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.printwriter
PrintWriter Class (Java.IO) | Microsoft Learn
[Android.Runtime.Register("java/io/PrintWriter", DoNotGenerateAcw=true)] public class PrintWriter : Java.IO.Writer
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 21 & JDK 21)
October 20, 2025 - Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › io › class-use › PrintWriter.html
Uses of Class java.io.PrintWriter (Java SE 19 & JDK 19 [build 1])
Sets the logging/tracing PrintWriter object that is used by the DriverManager and all drivers. Methods in java.util with parameters of type PrintWriter
🌐
Android Developers
developer.android.com › api reference › printwriter
PrintWriter | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 17 & JDK 17)
January 20, 2026 - Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › › java › io › class-use › PrintWriter.html
Uses of Class java.io.PrintWriter (Java Platform SE 8 )
October 20, 2025 - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
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.
🌐
Cmu
edelstein.pebbles.cs.cmu.edu › jadeite › main.php
PrintWriter (Java Platform SE 6)
The maximum number of arguments ... · public PrintWriter printf(String format, Object[] args) A convenience method to write a formatted string to this writer using the specified format string and arguments....