wood burning stoves 2.0*
The moose likes Java in General and the fly likes Text File Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Text File Help" Watch "Text File Help" New topic
Author

Text File Help

Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12
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

Joined: Feb 07, 2005
Posts: 2367
Could you post your full code now? Also could you explain again what you're trying to do?
Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12

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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 25, 2006
Posts: 12
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Text File Help
 
Similar Threads
Create .txt file
Program on Text Files
close() method; I/O class chaining
Writing to File
move file A to file B