Questions tagged [printstream]
175 questions
133
votes
6 answers
Java: Difference between PrintStream and PrintWriter
What is the difference between PrintStream and PrintWriter? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise,…

Martijn Courteaux
- 67,591
- 47
- 198
- 287
129
votes
6 answers
Java: PrintStream to String?
I have a function that takes an object of a certain type, and a PrintStream to which to print, and outputs a representation of that object. How can I capture this function's output in a String? Specifically, I want to use it as in a toString method.

Nick Heiner
- 119,074
- 188
- 476
- 699
80
votes
4 answers
Is multi-thread output from System.out.println interleaved
If multiple threads call System.out.println(String) without synchronization, can the output get interleaved? Or is the write of each line atomic? The API makes no mention of synchronization, so this seems possible, or is interleaved output prevented…

Ellen Spertus
- 6,576
- 9
- 50
- 101
49
votes
7 answers
Printing Runtime exec() OutputStream to console
I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done?
Here is some incomplete code:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import…

TheWolf
- 1,675
- 4
- 22
- 34
20
votes
3 answers
Why don't we close `System.out` Stream after using it?
I just want to know, we usually close streams at the end, but why don't we close System.out PrintStream with System.out.close()?

Eng.Fouad
- 115,165
- 71
- 313
- 417
19
votes
1 answer
Redirecting System.out to a TextArea in JavaFX
Update:
Still having the same issue, revised source of main app code:
http://pastebin.com/fLCwuMVq
There must be something in CoreTest that blocks the UI, but its doing all sorts of stuff (async xmlrpc requests, async http requests, file io etc), i…

Dreen
- 6,976
- 11
- 47
- 69
15
votes
4 answers
System.out.println vs PrintWriter
Is there a difference in using these two? When would you use one over the other?
System.out.println(result);
versus
PrintWriter out = new PrintWriter(System.out);
out.println(result);
out.flush();

Nikhil
- 797
- 6
- 12
- 30
12
votes
4 answers
"Turn off" the output stream
I'm using a wayward library that, unfortunately, prints information to System.out (or occasionally System.err). What's the simplest way to prevent this?
I've been thinking about creating an output stream to memory, replace System.out and err before…

Oak
- 26,231
- 8
- 93
- 152
12
votes
4 answers
Writing to console and text file
I found the code below from the internet, works but it doesn't write the printed console to omt.txt, it only writes the System.out.println statements after the second catch block.If you run the code once you will understand what I mean.All I want is…

Anarkie
- 657
- 3
- 19
- 46
11
votes
4 answers
PrintStream object out is initialized by null, how we call method on it?
I have seen in the System class that the out object (of type PrintStream) is initialized with a null value. How can we call method like System.out.prinln("");?
In System class out variable initialized like this way:
package java.lang;
public final…

Hitesh Bhalala
- 2,872
- 23
- 40
10
votes
1 answer
how to insert a new line character in a string to PrintStream then use a scanner to re-read the file
I have several classes designed to simulation a book catalog. I have a book class (isbn, title, etc...), a BookNode class, a BookCatalog which is a LinkedList of books and a driver class (gui).
My problem is that I have a toString() method in…

Sara
- 371
- 1
- 4
- 10
9
votes
1 answer
How to find out the occurred exception during using a PrintStream in Java
I have just read, that in Java the classes PrintStream and PrintWriter don't throw checked exceptions. Instead they are using a kind of an error flag which I can read invoking the method boolean checkError() (API link).
Now, I am asking myself how…

mrbela
- 4,477
- 9
- 44
- 79
8
votes
2 answers
what is the purpose of using System.out.println()
I was going through the internal implementation of System.out.println().Though I understood how this is working but couldn't figure out :
Why they decided to use the System class in the first place.
They could have directly used PrintStream class…

Ankush Dutt
- 143
- 8
8
votes
2 answers
Java Unicode to hex string
The code below gives me the Unicode string as கா
sysout = new PrintStream(System.out, true, "UTF-8");
sysout.println("\u0B95\u0bbe");
By giving கா as input, can I get the hex values as \u0B95 and \u0bbe?
PS: This is Tamil language.

user1611248
- 708
- 3
- 7
- 13
7
votes
2 answers
Why does PrintStream.close() end up getting called twice?
Somewhat to my surprise, the following code prints out "Close" twice. Running through the debugger, it seems MyPrintStream.close() calls super.close(), which ends up calling MyPrintStream.close() again.
import java.io.*;
public class…

Simon Nickerson
- 42,159
- 20
- 102
- 127