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 OverflowOracle
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.
Videos
10:29
Java Tutorial #79 - Java PrintStream Class with Examples (File ...
03:29
What is PrintStream in Java? | Java IO | Java Tutorial - YouTube
11:13
5.2 PrintStream - YouTube
09:42
23.9 : PrintStream in Java | Output Formatting [Abdul Bari] Java ...
07:59
2025 자바 강의(Java) 044 - PrintStream 객체의 역할 - YouTube
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, ...
Top answer 1 of 2
4
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));
}
2 of 2
0
import java.io.PrintStream;
public class Main2{
public static void main(String[]args){
String str = " The Big House is";
PrintStream ps = new PrintStream(System.out);
ps.printf("My name is : %s White", str);
ps.flush();
ps.close();
}
}
Output is : The Big House is White
The other way to use PrintStream is...
CharSequence cq = "The Big House is White";
PrintStream ps = new PrintStream(System.out);
ps.append(cq);
ps.flush();
ps.close();
}
}
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
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.
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
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.
Call +917738666252
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.