To get a string from the output of a PrintWriter, you can pass a StringWriter to a PrintWriter via the constructor:
@Test
public void writerTest(){
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
// use writer, e.g.:
writer.print("ABC");
writer.print("DEF");
writer.flush(); // flush is really optional here, as Writer calls the empty StringWriter.flush
String result = out.toString();
assertEquals("ABCDEF", result);
}
Answer from weston on Stack Overflow Top answer 1 of 7
61
To get a string from the output of a PrintWriter, you can pass a StringWriter to a PrintWriter via the constructor:
@Test
public void writerTest(){
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
// use writer, e.g.:
writer.print("ABC");
writer.print("DEF");
writer.flush(); // flush is really optional here, as Writer calls the empty StringWriter.flush
String result = out.toString();
assertEquals("ABCDEF", result);
}
2 of 7
13
Why not use StringWriter instead? I think this should be able to provide what you need.
So for example:
StringWriter strOut = new StringWriter();
...
String output = strOut.toString();
System.out.println(output);
Coderanch
coderanch.com › t › 582002 › java › read-content-Printwriter-object
How can i read the content of the Printwriter object (Java in General forum at Coderanch)
Short answer: you can't. You've created a PrintWriter that writes direct to System.out. It doesn't store the text you write to it, it just writes it out to System.out. Longer answer: but if you want to create a different PrintWriter, write to that, and then get the contents as a String, you ...
Programiz
programiz.com › java-programming › printwriter
Java PrintWriter (With Examples)
The printf() method can be used to print the formatted string. It includes 2 parameters: formatted string and arguments. For example, ... The formatted string includes both text and data. And, the arguments replace the data inside the formatted string. Hence the %d is replaced by 25. import ...
Tutorialspoint
tutorialspoint.com › home › java/io › java printwriter - print string
Java.io.PrintWriter.print( String s) Method
February 13, 2026 - The java.io.PrintWriter.print() method prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these ...
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.
YouTube
youtube.com › watch
Java Streams Using Strings - The StringWriter and PrintWriter Class - toString Method - APPFICIAL - YouTube
An input string stream is a Scanner object initialized from a String. An output string stream can be created by using the StringWriter and PrintWriter class....
Published April 27, 2018
Tabnine
tabnine.com › home page › code › java › java.io.printwriter
java.io.PrintWriter java code examples | Tabnine
List<HasWord> sent = SentenceUtils.toWordList("The", "slimy", "slug", "crawled", "over", "the", "long", ",", "green", "grass", "."); List<TaggedWord> taggedSent = tagger.tagSentence(sent); for (TaggedWord tw : taggedSent) { if (tw.tag().startsWith("JJ")) { pw.println(tw.word()); } } pw.close(); } ... StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); sw.toString(); // stack trace as a string
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 - The client may inquire as to whether any errors have occurred by invoking checkError(). This class always replaces malformed and unmappable character sequences with the charset's default replacement string. The CharsetEncoder class should be used when more control over the encoding process is required. ... The underlying character-output stream of this PrintWriter...
Delft Stack
delftstack.com › home › howto › java › java printwriter
How to Use Printwriter in Java | Delft Stack
February 2, 2024 - In the below example, we create a PrintWriter object and then create a loop to run three times and print the string with the value of i. Unlike the last example, the output is cleaner, and the line breaks after printing a line. import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterExample { public static void main(String[] args) throws FileNotFoundException { try { PrintWriter printWriter = new PrintWriter("test.txt"); for (int i = 1; i < 4; i++) { printWriter.println("This is Line no.
o7planning
o7planning.org › 13411 › java-printwriter
Java PrintWriter Tutorial with Examples | o7planning.org
The println(..) method is used to print a primitive value and a newline character into this PrintWriter. The flush() method is also called if the PrintWriter has auto flush mode. public void println(boolean x) public void println(char x) public void println(char[] x) public void ...
CodingTechRoom
codingtechroom.com › question › convert-printwriter-to-string-file
How to Convert a PrintWriter Output to a String or Write It to a File? - CodingTechRoom
PrintWriter printWriter = new PrintWriter(new StringWriter()); printWriter.println("Hello, World!"); String resultString = printWriter.toString(); In Java, the PrintWriter class is widely used for writing formatted text to output streams, including files.
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 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 ...
Mpg
resources.mpi-inf.mpg.de › d5 › teaching › ss05 › is05 › javadoc › java › io › PrintWriter.html
java.io Class PrintWriter
java.lang.Object java.io.Writer java.io.PrintWriter ... Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.
Narkive
comp.lang.java.help.narkive.com › dKrnNGj0 › what-s-the-diff-printwriter-vs-outputstreamwriter
What's the diff PrintWriter vs. OutputStreamWriter ?
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(....))); Is one method deprecated? Are all methods suitable for text files? PrintWriter is a convenience Writer that offers methods to handle conversion of Java data types to text automagically, as well as convenience methods to output the appropriate system-defendant line separator string.
BeginnersBook
beginnersbook.com › 2015 › 05 › java-writer-to-string-conversion
Java – Writer to String conversion
September 11, 2022 - 正在验证您的请求,请稍候…