*
The moose likes Beginning Java and the fly likes Declaring a PrintWriter object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring a PrintWriter object" Watch "Declaring a PrintWriter object" New topic
Author

Declaring a PrintWriter object

Malcolm Whitely
Greenhorn

Joined: Feb 06, 2003
Posts: 16
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

Joined: May 10, 2001
Posts: 265
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

Joined: Feb 06, 2003
Posts: 16
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

Joined: May 10, 2001
Posts: 265
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

Joined: Feb 06, 2003
Posts: 16
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

Joined: May 10, 2001
Posts: 265
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

Joined: Feb 06, 2003
Posts: 16
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

Joined: May 10, 2001
Posts: 265
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Declaring a PrintWriter object
 
Similar Threads
constructing a PrintWriter object
BufferedWriter, PrinterWriter etc
PrintWriter
How to write XML to a new file.
Source of Out Of Memory error