Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Appending to a .txt file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
Christophe has suggested exactly the same as I would have suggested.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic