Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing/fetching variables from a file

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, im trying to get my hands on reading and writing to file and Im kinda lost. I want to store variables in a file so I can fetch it later. Say I need to store 3 integers and 1 string, my first thought was to write to a .txt file like this:

5
22
23
Hello

Then I know what variable is in the first line and second line and so on.
Say I need to update the string to "Goodbye" how would I do that? I.e so the file would look like this:

5
22
23
Goodbye

I tried p.println(\n\n\n"Goodbye") but that doesn�t work. I also need to know how I would read the string i.e only line 4.

This is my code.


Is there maybe a better way I should be doing this?
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can use Properties and store the values in file as Key Value Pair. This way u can control the variables to great extend. I dont know about the performance part of this method. But still its effective.
I have a sample code u might want to refer.


The file looks like

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot!

I couldn�t have asked for a better solution!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to say the answer is probably read the file into memory, manipulate the values, write it back out. The Properties class automates a lot of that process so long as the name=value structure works for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!