This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.