Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Save data into textfile  RSS feed

 
fiddy chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to store the data from the Jtextfield into a textfile and retrieve it when necessary.. i am able to save the data into the textfile but whenever i saved a new data, the old data in the textfile will be overwrite by the new data. how i can manage to solve this problem.. thanks..
 
fiddy chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to store the data from the Jtextfield into a textfile and retrieve it when necessary.. i am able to save the data into the textfile but whenever i saved a new data, the old data in the textfile will be overwrite by the new data. how i can manage to solve this problem.. thanks.. i need help urgently.. thanks..
 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are using FileOutputStream to write to the file. What constructor are you using to create the FileOutputStream object? Use the one which asks you to specify whether you want to append to the file or not. The constructor's arguments precisely are FileOutputStream(File file, boolean append). Set the second argument, which is a boolean, to true when you create the FileOutputStream object.
Another un-advisable way of doing this is to read the contents of the file in a buffer when you initialize. Append whatever data you read from the JTextField etc. into that buffer and write that buffer back to the file when you need to. However, this method is not at all advisable cuz if the file size is in high mbs you may have to go to sleep and come back to see the operation completed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!