| Author |
Printing Int Array to File using PrintWriter
|
Jim Hester
Ranch Hand
Joined: Sep 19, 2009
Posts: 36
|
|
I'm hoping this is a simple fix and I'm just overlooking something. In the PrintArray method, I really want to send that output to the file, but pw.print won't work. Does the method not inherit or know that I've already initialized the PrintWriter?? What's a way to fix this other than hardcoding it in?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32662
|
|
Why don't you use a Formatter if you are writing plain text to a file? It's easier to use than a PrintWriter?
Anyway, wouldn't you use a BufferedWriter wrapped round a FileWriter?
Why are you starting the names of your methods with Capital Letters?
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
how about ...
One thing... the Java Code Convention defines that class names and constructors start with a capital letter, methods don't. ;-)
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32662
|
|
That looks pretty disastrous, I am afraid.
Just from memory, so you will have to check the details in the API or the Java™ TutorialsI am pretty sure I have got most of the method names wrong, but you can check that in the API. You will notice that you can add "true" as a parameter to the FileWriter constructor; it tells you in the API what that means. A Formatter is still probably easier to use. You will also have to put something in the catch. And also work out why I have put two "try"s inside each other with a "finally".
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
Sebastian Janisch wrote:how about ...
One thing... the Java Code Convention defines that class names and constructors start with a capital letter, methods don't. ;-)
I forgot one thing..
either the print array method returns a string with whatever needs to be written to the file or you set the system out to your PrintWriter and leave the method the way it is ...
that would be
|
 |
Jim Hester
Ranch Hand
Joined: Sep 19, 2009
Posts: 36
|
|
Thanks much. I just modified the method so it returned a string, and went from there. I also fixed the naming conventions. Final code below. Thanks!!
|
 |
Evan Caballero
Ranch Hand
Joined: Dec 10, 2009
Posts: 59
|
|
You should re-write your printArray method like this, because String is immutable. A new String instance is created each time you call a method on it.
The use of StringBuffer is preferred ;)
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
In your printArray method: use StringBuilder instead of String.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32662
|
|
|
Agree with SJ. StringBuilder is usually better than StringBuffer.
|
 |
Evan Caballero
Ranch Hand
Joined: Dec 10, 2009
Posts: 59
|
|
|
Isn't StringBuffer a sub class of StringBuilder ?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32662
|
|
|
No
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
stringbuilder vs stringbuffer
|
 |
Evan Caballero
Ranch Hand
Joined: Dec 10, 2009
Posts: 59
|
|
|
nice, thanks ;)
|
 |
Jim Hester
Ranch Hand
Joined: Sep 19, 2009
Posts: 36
|
|
|
Thanks! Wasn't aware of StringBuilder. Made the appropriate changes.
|
 |
 |
|
|
subject: Printing Int Array to File using PrintWriter
|
|
|