• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending to a .txt file  RSS feed

 
jack pashley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make an organizer for myself and my friends at school. At the moment i have become stuck at this part. What I want it to do is add a new line of text when i click a button, this works but when i close the program, change what it enters and run it again it will still just erase the lines if codes in the .txt file and just put in that new line.

For example I want it to go
END OF FIRST LESSON
Homework:cibfsibfibfsfiufsfhi
END OF SECOND LESSON
Homework:ebfisfiuhefsiuhefiushif

But what it is doing is just erasing the lines of text and adding the second line of code pleas help.

this is my code
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different ways to output data to a file. Check the FileWriter class, which allows you to append to an existing file instead of overwriting it.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
Christophe has suggested exactly the same as I would have suggested.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jack pashley wrote:I am trying to make an organizer for myself and my friends at school.
...
But what it is doing is just erasing the lines of text and adding the second line of code pleas help.

I think Christophe has answered your basic query, but it's worth remembering that file I/O is extremely slow, so if I were doing something like this, I'd be tempted to read the whole thing into a List or Map of some kind and do all "updates" internally until the user specifically requests that they be "saved" - at which point I'd just write the whole darn thing out, overwriting the previous version.

It's also possible that the filesystem to which it's being written might be able to optimize this approach a bit better.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!