• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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: 80747
486
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic