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

Writing (INSERTING) to a previously created text file  RSS feed

 
Anthony Yulo
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I write to a previously created text file... I just want to append new Strings to the end of text files...
I've read a book describing that you can't insert or remove to a flat file, you either read to it or create a new one and write from scratch..
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To append after the end of a file, you can use a FileOutputStream or FileWriter, unsing one of the constructors that takes a boolean argument (which controls whether you append or overwrite). E.g.

I've read a book describing that you can't insert or remove to a flat file, you either read to it or create a new one and write from scratch..
That's basically true for the standard stream-based IO classes (OutputStreams and Writers) - aside from appending to the very end, which is easy as discussed above. To insert anywhere else in a file using streams, you basically have to write an entire new file, with the modifications.
However, it's also possible to use a RandomAccessFile or a FileChannel (found in JDK 1.4) to perform inserts a little more efficiently (maybe). It's a bit messy - if I've got a 1000 byte file, and I want to insert 1 byte at position 500, I first need to shift all bytes from position 500-999 to the right by one, so they occupy 501-1000. Then write the single new byte at 500. If I want to insert two bytes at 500, I could simply repeat this process - shifting 500-999 to 501-1000, then 501-1000 to 502-1001 - but this is very inefficient. Much better to just shift everything by 2 bytes in the first place, 500-999 to 502-1001 - then write the two new bytes at 500-501. And so on - to insert 100 bytes, you prefer to shift existing bytes by 100, once, rather than shifting by 1, 100 times. So it's very helpful to know in advance what all the changes you make will be, and how many bytes they require. Depending on your application, maybe this is possible, maybe not. It can get very complex. But I wanted to give you some idea of what's involved, since you brought it up.
 
Anthony Yulo
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tnx jim.. i'm making a mini chat program... i just need to add additional usernames and passwords in my text file... when someone new registers... so i just need to append to the end of the file... I thought was hard... you just only have to use another constructor... thanks..
And also thanks for the informations on RandomAccessfIles... another info added to my java knowledge..
thanks
 
Anthony Yulo
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ei!! one other thing....
When i'm appending to the text file... the text that i inserted appears after the last character of the file... But i wanted to insert a new line... so the text i'm appending must be a new line...
I tried entering "\n" + my appended string but the file recognizes \n as a square like ([]) character....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!