• Post Reply Bookmark Topic Watch Topic
  • New Topic

saving data to a file that has been entered  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am trying to save the data a person enters to a file. please see my code below. It is compiling however the file created only shows largest =89, i want the file to show all the data a person enters.
any help would be appreciated.



thanks
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the main method being about 100× longer than it ought to be

You will never get that amount of code to work. What you should do is to create an object which encapsulates all those data, pass it to a method which writes to the file, and write the object. Maybe use its toString method to give you a line to write. That should be in a separate method, maybe in a utility class.

You ought not to use a bare file writer to write to a text file. You should wrap it in a buffered writer, formatter or similar. Go through the Java Tutorials section. You should also go through the documentation for file writer, looking carefully at the constructor choices. If you read the descriptions of the constructors, you will see that there are options for different behaviours.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree that you couldn't get the code to work as is; it's not particularly long or intricate.

If you open a file, the default behavior is to overwrite its contents. If you want to append to its contents instead, then you need to specify that especially. All you need to do is to use a different FileWriter constructor.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

I got it to work.

Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!