Solved

print to output window

Posted on 2001-06-17
8
170 Views
Last Modified: 2010-04-16
Hi experts,
I have a question about "printing to output window". if i want to print some stuff to the output window, one way is:
System.out.println("hello");

an alternative way is:
FileWriter fw = new FileWriter(FileDescriptor.out);
PrintWriter pw = new PrintWriter(fw);
pw.println("hello");

any difference between these two approaches? Thank you for your help.

ken
0
Comment
Question by:ken021600
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6200546
System.out is a PrintStream, as opposed to a PrintWriter so there are differences. Check the javadoc for more details.
Your example uses a println in one case, and a print in the other so this will also cause a difference.

0
 
LVL 3

Expert Comment

by:Sendoh
ID: 6200672
hi !
think the other differences is ur program will take up more resources as the System.out is already define, no point define a new priwriter just for this purposes(printing hello to screen) :P

haha
0
 

Author Comment

by:ken021600
ID: 6201693
Thanks a lot for your help.

anyways,i have to point out:
1)"println" and "print" are not the issue, just a typo.

2)I've already checked out the java API. I posted this question here coz i didn't get anything from reading the API, not coz i'm lazy...so what on earth are the differences between PrintStream and PrintWriter?

3)printing "hello" to the standard output is just a simple way to ask this question. it makes life easier without losing the point...

thanks,
ken
0
 
LVL 92

Expert Comment

by:objects
ID: 6204383
They handle new line and flushing differently:

PrintStream uses '\n' as a new line character.
PrintWriter uses the system defined new line character.

PrintStream autoflushs whenever a new line character appears in input.
PrintWriter only autoflushs if a println() is called.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ken021600
ID: 6205359
aha you are Aussie! (i live in Sydney anyway)i didn't know that till just now...i had a look at your profile.

Thanks for your help. but why did those Java developers bother to develop two ways to do it?

thanks,
ken
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6205669
I'm in Sydney too :)
Not exactly sure on the reason for two ways, but could be so that for each OutputStream class, there is a corresponding Writer class.

0
 

Author Comment

by:ken021600
ID: 6205700
cheers mate!
ken
0
 
LVL 92

Expert Comment

by:objects
ID: 6205735
Only a B :-)

Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
custom annotations 9 32
print map entry 34 55
how to install java on RHEL image on EC2 4 7
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now