aspose file tools*
The moose likes Beginning Java and the fly likes writing data to local file...while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing data to local file...while loop" Watch "writing data to local file...while loop" New topic
Author

writing data to local file...while loop

william macleod
Greenhorn

Joined: Sep 28, 2006
Posts: 4
Hi, this is my first post here. I've enjoyed reading back into the archives and have gotten a lot of helpful info. I'm in the fourth week of an online jave course. I havent done anything with my computer previously other than run software and check email. I am having a lot of fun and sleepless nights with Java so far. Everything so far has come to me after a little thinking, googling and reading the Sun site and other forums, but now I'm stuck. I just cant seem to figure out what to do.

The project is... using a while loop, take user data till a sentinel string is entered and print the data to a local file. Sounded really easy at first. I managed to get the while loop working, but I cant for the life of me figure out how to get the data collected into one file. All the data is entered as String type. I've seen alot of examples using loops of various kinds on primitive type data, but not much on Strings. Here is what I have so far:





I would appreciate any kind of shove in the right direction.

Thanks
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
First thing I would do is wrap PrintWriter around FileWriter, then set the append flag to what you need.

PrintWriter outFile = new PrintWriter(new FileWriter("orderOut.dat",false));//allows for overwriting the previous file.

Change to true if you want to append.

Then in the loop, after you get the input use the proper method in PrintWriter. hint you need to print a line. Check the API for a good candidate.

Just one simple line in the loop will solve this, if you put both inputs per loop on the same line. If they go on seperate lines, then 2 identical lines will be needed(or you can create a very small method to print to the file.
[ September 28, 2006: Message edited by: Robert Hill ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You're very near, pal. You just forgot to write you item and price into the outFile...




...before asking for a new item.
william macleod
Greenhorn

Joined: Sep 28, 2006
Posts: 4
Thanks a bunch fellas. I tried out Edwin's suggestion first. Because it seemed like I could try it out immediatly.....Great. Damn it seems so simple now!

Is this what you had in mind??





It works great!!!

I think I was confused on the issue of what would overwrite the local file. I thought every time you added something new it would overwrite what ever was there previously. But now, I'm thinking... the file will only be overwritten when the file is closed and then opened again.......is this correct?

First thing I would do is wrap PrintWriter around FileWriter, then set the append flag to what you need.

PrintWriter outFile = new PrintWriter(new FileWriter("orderOut.dat",false));//allows for overwriting the previous file.

Change to true if you want to append.


Does this mean that I can re-open a file already written and have a choice to overwrite (is this a default?) or to add to the existing file?

While Edwin's was very easy to insinuate into what I already had written, I'm curious as to which way of doing it is "better". Most functional, most like the real world.

I thank you both for a push over my hump. After I post this I am going to get Robert's suggestion into my editor and play around with it. As a matter of fact play around with both of them and see what I can do and not do. As far as different ways to format, ect.

Thanks again
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Personally, I would use println( String x) from the PrintWriter class since it automatically terminates the line.

As for overwriting/appending. Unless you set it to overwrite it will continue to add to the file.

I don't remember offhand if a PrintWriter object will append or overwrite by default. You can play around with it.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing data to local file...while loop
 
Similar Threads
Customer Order project
text formatting using java
2d Array problem gasprices
Writing to a file
Calling methods using dynamic binding