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

Opening a file that already exists  RSS feed

 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone. i'm trying to open a file for writing that already exists in the program's current directory. If I run my program once, it writes correctly to the file. If I then start the program again, it seems to recreate the file over the old file (thus losing the data that was written to it previously). i can't seem to find anywhere on tutorials how to simply OPEN a file for write rather than CREATE a new file for write.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You open a file whether it exists or not. What you want to do is append to the existing file.
Take a look at the constructors for java.io.FileOutputStream or java.io.FileWriter. I think you will see what to do.
 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahhh..i see...how silly of me
 
srinivas daroori
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My personal experience is it is always better to use RandomAccessFile, if you need to append to an existing file. Go to the end of the file by
RandomAccessFile.seek() and then write to it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!