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

ugrent help needed  RSS feed

 
padma patil
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to implement search and replece function of notepad in Java.
1)can anybody explain me / give an idea of, how to search for a text in a file. is there any fseek command for searching or I need to read the line(readln()) and serach word by word?
2) Once I find the text wanted to replace the text with new text and store it in the file.
how can implement this?
I really appreciate your feedback.
Thanks,
padmashree
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Padma,
Unfortunately, there is no feature in Java to perform file searching. You will need to read in the file and use string methods. Once you have changed the text you can then write it back out.
The only I/O component that supports seeking in files is RandomAccessFile, but it doesn't support searching for something, just setting the file position at some byte.
Regards,
Manfred.
 
padma patil
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Now I am facing different problem with this find and replace program.
This is how I am implementing.
1)reading each line from file1.txt into string
2)finding the text in the string and replacing it with new string.
3)modified string I am writing back to file1.txt.
It works fine if the length of the replace with text is more that find text. But if it's less then after writing into file it keeps extra characters.(As file write method overwrites the content char by char).
Is there any way I can get rid of extra chars?

thanks,
padmashree
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!