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).
🌐
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 ...
🌐
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
🌐
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
🌐
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.
Top answer
1 of 7
38

According to coderanch.com, if we combine the answers we get:

FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.

PrintWriter & FileWriter.

Similarities

  1. Both extend from Writer.
  2. Both are character representation classes, that means they work with characters and convert them to bytes using default charset.

Differences

  1. FileWriter throws IOException in case of any IO failure, this is a checked exception.
  2. None of the PrintWriter methods throw IOExceptions, instead they set a boolean flag which can be obtained using checkError().
  3. PrintWriter has an optional constructor you may use to enable auto-flushing when specific methods are called. No such option exists in FileWriter.
  4. When writing to files, FileWriter has an optional constructor which allows it to append to the existing file when the "write()" method is called.

Difference between PrintStream and OutputStream: Similar to the explanation above, just replace character with byte.

PrintWriter has following methods :

close()
flush()
format()
printf()
print()
println()
write()

and constructors are :

File (as of Java 5)
String (as of Java 5)
OutputStream
Writer

while FileWriter having following methods :

close()
flush()
write()

and constructors are :

File
String 

Link: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter

2 of 7
12

Both of these use a FileOutputStream internally:

public PrintWriter(File file) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
     false);
}



public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}

but the main difference is that PrintWriter offers special methods:

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

🌐
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.
🌐
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
🌐
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 · 中文 – 简体
🌐
Reddit
reddit.com › r/learnjava › printwriter system.out differences...?
r/learnjava on Reddit: PrintWriter System.out differences...?
October 20, 2020 -

So I made some code to test a theory, and it disproved my theory. Here it is:

import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) {
        PrintWriter writer = new PrintWriter(System.out);

        System.out.println("HI");
        writer.println("HI");
        writer.flush();
    }
}

Now here's the context for my question. I was taught that System.out is a stream object, and that the console grabs from the System.out stream and puts those contents on its stream. I was then taught that PrintWriter is an class that functions essentially identically to System.out, except that you can direct your destination stream. So I hypothesized, "If you made a PrintWriter object and sent the System.out stream in as its destination, would that be identical to just using System.out?

As it turns out, no, but I still don't fully understand why. What I do understand is that if I use the flush() method, it does essentially work the same. So my question is this:

When you use System.out.println() , does the method println() also tell the console to grab from the buffer so its not left in the System.out stream? And from the other perspective, when you use writer.println() (writer being the PrintWriter object in this context), does the println() method NOT tell the destination stream to grab from the buffer (until the flush() method is used)?

Any information helps, feel free to tell me that my interpretation is wrong or that my hypothesis is wrong. Or you can tell me anything really, tell me how your day is going. Any information helps, the more I know the better a programmer I can become :)

🌐
Coderanch
coderanch.com › t › 649398 › java › Writing-console-PrintWriter
Writing to the console with PrintWriter (Beginning Java forum at Coderanch)
May 1, 2015 - I thought, we need to flush to avoid problem with subsequent operations. So if we just have a single statement of printwriter.println() - without flushing - we shouldn't face any problem, right?
🌐
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 - The PrintWriter class prints formatted representations of objects to a text-output stream. Methods in this class never throw I/O exceptions. However, there is the possibility that some of its constructors may.
🌐
Baeldung
baeldung.com › home › java › java io › printstream vs printwriter in java
PrintStream vs PrintWriter in Java | Baeldung
January 8, 2024 - However, in this case, Java does not convert the string into bytes, but it internally translates each character in the stream into its corresponding Unicode encoding: public class PrintStreamWriter { public static void main (String[] args) throws IOException { PrintWriter out = new PrintWriter("TestFile.txt"); out.print("Hello, world!"); out.flush(); out.close(); } }
🌐
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 // to be printed in the stream String string = "GFG"; // print the string // to this writer using print() method // This will put the string in the stream // till it is printed on the console writer.println(string); writer.flush(); } catch (Exception e) { System.out.println(e); } } }
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › printwriter-printstring-method-in-java-with-examples
PrintWriter print(String) method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // PrintWriter print(String) method import java.io.*; class GFG { public static void main(String[] args) { try { // Create a PrintWriter instance PrintWriter writer = new PrintWriter(System.out); // Print the String value 'GFG' // to this stream using print() method // This will put the StringValue in the // stream till it is printed on the console writer.print("GFG"); writer.flush(); } catch (Exception e) { System.out.println(e); } } }
🌐
O'Reilly
oreilly.com › library › view › java-for-dummies › 9781118239742 › a68_07_9781118239742-ch04.html
PrintWriter Class - Java For Dummies Quick Reference [Book]
June 5, 2012 - PrintWriter Class Package: java.io The PrintWriter class lets you write data to an output stream. Although you can connect a PrintWriter to any object that implements Writer, you’ll... - Selection from Java For Dummies Quick Reference [Book]
Author   Doug Lowe
Published   2012
Pages   288