• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text File Help

 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write to a text file multiple times while saving the previously entered info. I've been searching the web for solutions, but haven't been able to find any. The problem I am having right now is that the output is not comming out correctly.

Thank you.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using the constructor of PrinterWriter that accepts a FileWriter and a boolean, just use the one that accepts just a FileWriter.

When creating the FileWriter use the constructor that accepts a String and boolean.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closely at this little loop:

When you read in the while condition what happens to the line you read? See if this makes sense:

Back in mainframe days we used to call that a "primer read" plus the read in the loop. But nowadays we try to eliminate duplication and this is a more common syntax:

The first time you read this it probably makes no sense. The trick is that an assignment statement like "line = br.readLine()" also returns the value that was assigned, rather like a function. So we can stuff the value of the readLine() into the variable "line" and check it for null in one line of code.

There is another read() right before the loop. Does that one also throw away some data?

After all that, we probably don't need this read and write scheme at all. Look into the optional second argument on the FileWriter constructor.
 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the suggestions! The output is printing out all the lines now, but the textfile now has large gaps between text. I can't figure out why it is doing this.

Also, you mentioned looking into the filewriter's alternate constructor. How do I do this? My teacher never taught me how to. Thanks again.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post your full code now? Also could you explain again what you're trying to do?
 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am just trying to write these statements to a textfile, and save previous writes. The text I posted above was after running the program three times.
[ March 25, 2006: Message edited by: Al Stumpel ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you are getting the blank is because in the for loop you run through every element in the list and print a blank line in the file.

The constructor of the FileWriter that accepts a String and a boolean, if the boolean is true, will open the file for appending.

If you use the constructor of FileWriter that just accepts a String, if the file already exists, it will be overridden and you'll lose what's there.
 
Al Stumpel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks it's working properly now. I don't know why I even had the for loop in the first place. I took out the loop and now it works.
P.S. Now I understand what you mean about the first part being a waste with the second version of the constructor. I just removed that whole first try catch. Thanks again!

[ March 25, 2006: Message edited by: Al Stumpel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic