• Post Reply Bookmark Topic Watch Topic
  • New Topic

inserting text at some place in file  RSS feed

 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to insert some text in a ".java" file at a specified location. How can i do it? I just want to open a ".java" file and insert the text at a specified location. I have tried to do so by using RandomAccessFile class but it is inserting text in the form of codes. Can anyone help me ASAP. thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't be able to insert with RandomAccessFile. If you seek a location and start writing you'll overwrite what's aleady there. About the only way to insert is to read the whole file and write a new copy. Here's a safe strategy for that:
read original
write new-temp to completion
rename original to backup
rename new-temp to original
erase backup
Your other issue about "codes" may be because RandomAccessFile stores bytes, not Strings. How did you write (what methods, etc) and what did you see?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali, welcome to JavaRanch!
We don't have many rules, but we do have our Naming Policy, which asks for you to have both a first and last name. Doesn't have to be your real last name (though we prefer that), but you need to have one and it needs to be realistic.
Please change your display name in your profile to fit with this policy.
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!