• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaring a PrintWriter object

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic