• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update a file when a token occurs  RSS feed

 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I need to read and modify a text file. I am reading using bufferedReader's readLine method as I need to read line by line. If the line that i am reading contains a particular string, i need to modify that line by inserting some text at a particular position. If it doesnt contains the string, I wont do anything. Kindly inform, what is the way to achieve this?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the size of what you are updating is a fixed size you can update it in-place by using RandomAccessFile. If it is a string of text without a fixed size you will have to read the file in, parse out the text that needs replacing, insert the new text and write out the new file.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!