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

Declaring a PrintWriter object

 
Malcolm Whitely
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am trying to declare a new PrintWriter object as so:
PrintWriter pw= = new PrintWriter(OutputStream out);
which appears correct, but on compiling I get the 4 error message including:
") expected" and "cannot resolve symbol"
I've also tried "PrintWriter pw = new PrintWriter(Writer out, boolean autoFlush)" but to no avail.
Hope someone can help.
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create a PrintWriter by initializing it with an instance of OutputStream. Something like this:

Then you can do what you want with your writer.
 
Malcolm Whitely
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Am I right in thinking that PrintWriter can be used alongside the println method to print characters on screen? If so, then how would the code differ?
One other thing, I tried the code you suggested but get a NullPointerException when I try to write a a string to the output file.
Thanks in advance and looking forward to hear from you.
[ February 20, 2003: Message edited by: Malcolm Whitely ]
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's something I just threw together that should give you an idea.

As for your println question:
System.out.println(String x) prints to the standard output stream, which is a PrintStream.
The System class provides a static instance of this PrintStream for convenience, so you can call System.out.println();
 
Malcolm Whitely
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for the reply. It cleared out a few things for me.
At the moment, I've got a main() method and an output() method, which writes the contents of an array to a file. I've tried passing in my PrintWriter object from the main() method but the writing never seems to occur from within the output() method.
Here is my code:

System.out.println() works fine in output() but pwrtr.println() doesn't. Another thing worth mentioning is that "Hello" which is executed in main() is written to file.
Thanks for the help. I really appreciate it.
[ February 21, 2003: Message edited by: Malcolm Whitely ]
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a couple of problems right off. First, you don't have any data in the String[] that is passed to your output method. Second, even if you have data, you aren't flushing the writer, so you still won't get anything.
I modified your code a little. Take a look at it and note the things I changed.
 
Malcolm Whitely
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked great Chad! As you suggested, the flush() method, which I have since read about, was the reason the data was not written to file.
I have noticed that the Java API is not as kind to beginners as Microsoft's MSDN which elaborately explains everything. Is there a book, more of a reference one with breakdowns of packages and classes, that you would recommend?

Thanks and best regards.
Malcolm
[ February 21, 2003: Message edited by: Malcolm Whitely ]
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking in Java gives some nice examples for some of the more commonly used classes. The link is to a free html version. When I started learning java, I read it, along with Core Java I and II.
Once you get the hang of how to use the Java Documentation, you will find it more useful.
Good Luck
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic