First of all, don't reinitialize the PrintStream object in every loop iteration.

To write to a file, simply call PrintStream's println method. You may not realise it, but when you print to the console, this is exactly what you are doing. System.out is a PrintStream object.

PrintStream out =
    new PrintStream(new File("stuff.txt"));
while (in.hasNext()) {
    String word = in.next();
    out.println(convert(word));
}
Answer from Jade on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › PrintStream.html
PrintStream (Java Platform SE 8 )
2 weeks ago - Java™ Platform Standard Ed. 8 ... A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, ...
🌐
Programiz
programiz.com › java-programming › printstream
Java PrintStream (With Examples)
Become a certified Java programmer. Try Programiz PRO! ... The PrintStream class of the java.io package can be used to write output data in commonly readable form (text) instead of bytes.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.printstream
PrintStream Class (Java.IO) | Microsoft Learn
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › PrintStream.html
PrintStream (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, ...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › PrintStream.html
PrintStream (Java SE 11 & JDK 11 )
January 20, 2026 - java.io.PrintStream · All Implemented ... implements Appendable, Closeable · A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently....
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › PrintStream.html
PrintStream (Java SE 17 & JDK 17)
January 20, 2026 - A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › io › PrintStream.java
jdk/src/java.base/share/classes/java/io/PrintStream.java at master · openjdk/jdk
* <p> All characters printed by a {@code PrintStream} are converted into · * bytes using the given encoding or charset, or the default charset if not · * specified. * The {@link PrintWriter} class should be used in situations that require · * writing characters rather than bytes. * * <p> This class always replaces malformed and unmappable character sequences · * with the charset's default replacement string. * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more ·
Author   openjdk
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-printstream-class-java-set-1
Java.io.Printstream Class in Java | Set 1 - GeeksforGeeks
September 12, 2023 - A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations ...
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › io › PrintStream.html
PrintStream (Java SE 26 & JDK 26)
March 16, 2026 - Creates a new print stream, without automatic line flushing, with the specified OutputStream. Characters written to the stream are converted to bytes using the default charset, or where out is a PrintStream, the charset used by the print stream.
🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › ch1-system-print.html
1.7 From the Java Library: System and PrintStream
Java comes with a library of classes, organized into packages, that are used to perform common tasks. In this and similar sections in subsequent chapters, we will feature some of the most useful built-in classes. In this section we introduce the System and PrintStream classes, which are used for printing a program’s output.
🌐
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 OutputStream. Characters written to the stream are converted to bytes using the default charset, or where out is a PrintStream, the charset used by the print stream.
🌐
CodeGym
codegym.cc › java blog › java io & nio › java printstream class
Java PrintStream Class
October 11, 2023 - PrintStream(String outputFileName) throws FileNotFoundException For example, we can pass the name of the output file to the PrintStream constructor. Alternatively, we can pass a File object. Let's look at some examples to see how this works: import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; public class Main { public static void main(String arr[]) throws FileNotFoundException { PrintStream filePrintStream = new PrintStream(new File("C:\\Users\\Username\\Desktop\\test.txt")); filePrintStream.println(222); filePrintStream.println("Hello world"); filePrintStream.println(false); } } This code will create a test.txt file on the desktop (if it doesn't already exist) and sequentially write our number, string, and boolean to it.
🌐
Baeldung
baeldung.com › home › java › java string › java printstream to string
Java PrintStream to String | Baeldung
January 5, 2024 - This article provides several ways of converting a PrintStream to a String in Java.The approaches include using ByteArrayOutputStream, a custom output stream, and Apache Commons IO.
🌐
Jenkov
jenkov.com › tutorials › java-io › printstream.html
Java IO: PrintStream
September 3, 2015 - The Java PrintStream class (java.io.PrintStream) enables you to write formatted data to an underlying OutputStream. The PrintStream class can format primitive types like int, long etc. formatted as text, rather than as their byte values.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.printstream.-ctor
PrintStream Constructor (Java.IO) | Microsoft Learn
[Android.Runtime.Register(".ctor", "(Ljava/lang/String;Ljava/lang/String;)V", "")] public PrintStream(string? fileName, string? csn); [<Android.Runtime.Register(".ctor", "(Ljava/lang/String;Ljava/lang/String;)V", "")>] new Java.IO.PrintStream : string * string -> Java.IO.PrintStream
🌐
Coding Shuttle
codingshuttle.com › home › handbooks › java programming handbook › java printstream class
Java PrintStream Class | Coding Shuttle
April 9, 2025 - This blog explains Java’s PrintStream class, highlighting how to use its key methods like print, println, printf, and write with real-world examples. It also covers how PrintStream simplifies writing formatted, human-readable output to files and the console.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java printstream
Java PrintStream | Top 11 Methods Used in Java PrintStream with Example
May 10, 2024 - Java PrintStream has the capacity to print the depiction of many data values and adds functionality to a different output stream.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Classpath
developer.classpath.org › doc › java › io › PrintStream-source.html
Source for java.io.PrintStream (GNU Classpath 0.95 Documentation)
1: /* PrintStream.java -- OutputStream for printing output 2: Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005, 2006 3: Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath.
🌐
TechVidvan
techvidvan.com › tutorials › java-printstream-class
Java PrintStream Class with Examples - TechVidvan
May 6, 2024 - Unlike other output streams, PrintStream converts primitive data (integer, character) to text format instead of bytes.