Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about a program

 
alan fisher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class ChangeSystemOut {
public static void main(String[] args) {
PrintWriter out =
new PrintWriter(System.out,true);
out.println("Hello, world");
}
} ///:~
It's a program from <Thinking in java>.
When I use PringWriter(System.out),there's no result.why?
 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class ChangeSystemOut {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out,true);
out.println("Hello, world");
}
}
Tested and it works... Not sure why it's not working for you. What are the error meesages?
It would be easier to do it this way:
public class ChangeSystemOut {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use PringWriter(System.out),there's no result.why?

Hi Ken,
I think alan is asking about another constructor which takes only one argument as System.out. Pls check the below code.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add an out.flush() - the PrintWriter may buffer output for performance reasons - flush is taking care of writing the buffer to the actual output target, as is setting the autoflush property to true.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi alan fisher,
Please check the code below. that works fine after adding the flush().
Thanks folks..
import java.io.*;
public class ChangeSystemOut
{
public static void main(String[] args)
{
PrintWriter out = new PrintWriter(System.out,true);
out.println("Hello, world"); //This is printed
PrintWriter out1 = new PrintWriter(System.out);
out1.println("Hello, world1"); //This is also getting printed after using flush()
out1.flush();
}
} ///:~
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic