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 › en › java › javase › 21 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 21 & JDK 21)
January 20, 2026 - 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 › 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.
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 26 & JDK 26)
March 16, 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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › class-use › PrintWriter.html
Uses of Class java.io.PrintWriter (Java SE 21 & JDK 21)
April 15, 2025 - Sets the logging/tracing PrintWriter object that is used by the DriverManager and all drivers. Methods in java.util with parameters of type PrintWriter
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › io › PrintWriter.html
PrintWriter (Java SE 20 & JDK 20)
July 10, 2023 - 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
🌐
DZone
dzone.com › coding › java › java printwriter with example
Java PrintWriter With Example
February 19, 2020 - One important thing about the PrintWriter class is that it never throws any I/O exceptions. However, its constructor does in case of an error. This class has a method checkError(), which can be invoked by the client to check whether any error is occurred. You may also like: Java 8 (A Comprehensive Look): Part 1.1 - Lambdas Under the Hood
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › io › PrintWriter.java
jdk7u-jdk/src/share/classes/java/io/PrintWriter.java at master · openjdk-mirror/jdk7u-jdk
* Creates a new PrintWriter, without automatic line flushing, from an · * existing OutputStream. This convenience constructor creates the · * necessary intermediate OutputStreamWriter, which will convert characters · * into ...
Author   openjdk-mirror
🌐
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.
🌐
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, from an existing OutputStream.
🌐
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)
The PrintWriter class provides various methods that allow us to print data to the output. ... import java.io.PrintWriter; class Main { public static void main(String[] args) { String data = "This is a text inside the file."; try { PrintWriter output = new PrintWriter("output.txt"); ...
🌐
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 › en › java › javase › 21 › docs › api › java.base › java › io › PrintStream.html
PrintStream (Java SE 21 & JDK 21)
January 20, 2026 - Creates a new print stream, 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.
🌐
OpenJDK
cr.openjdk.org › ~jlaskey › templates › docs › api › java.base › java › io › Writer.html
Writer (Java SE 21 & JDK 21 [ad-hoc build])
java.io.Writer · All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable · Direct Known Subclasses: BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter · public abstract class Writer extends Object implements Appendable, Closeable, Flushable ·