Hello. My application must have a component for replacing text in text files, i.e. I give it the file, what to replace (old text and new text) and it gives me the file I want. Also there must be some statistics. That is what replacements, how many times and where in the file they were made. Does any one know something ready for this ? Thanks in advance.
Hi Grigory, Welcome to JavaRanch. That really shouldn't be all that tough to accomplish. Your best bet is probably to wrap the file in a BufferedReader and read in a line at a time (assuming you don't want to change a newline) then parse the line for for old text and change it to new text. Each line could then be inserted into a List. As text is substituted you would update your statistics. If new text consists of multiple words then you will have to look ahead one or more lines.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: Feb 21, 2003
I'm sorry if posted this question not to a proper forum. But my problem is that I *want* to replace newlines.
Joined: Jan 30, 2002
I *want* to replace newlines.
Then the same basic architechture would apply. You would just need to look ahead as many lines as necessary to find your matches. You could even read the whole file into one big String which would make parsing much easier but might run the risk of an OutOfMemory error.